:root {
  --bg: #0f172a;
  --sidebar: #111827;
  --sidebar-hover: #1f2937;
  --sidebar-active: #2563eb;
  --panel: #f4f5f7;
  --card: #ffffff;
  --border: #e3e6ea;
  --text: #1f2933;
  --muted: #6b7280;
  --accent: #2563eb;
  --out-bubble: #2563eb;
  --in-bubble: #e9ebee;
  --danger: #dc2626;
  --ok: #16a34a;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--panel);
  font-size: 14px;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
code {
  background: rgba(0,0,0,0.06); padding: 1px 5px; border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px;
}

/* ── banner ─────────────────────────────────────────── */
.banner {
  background: #fef3c7; color: #92400e; padding: 10px 16px;
  font-size: 13px; border-bottom: 1px solid #fcd34d;
}
.banner.error { background: #fee2e2; color: #991b1b; border-color: #fca5a5; }

/* ── layout ─────────────────────────────────────────── */
.app { display: flex; height: 100vh; }

/* ── sidebar ────────────────────────────────────────── */
.sidebar {
  width: 320px; min-width: 320px; background: var(--sidebar); color: #e5e7eb;
  display: flex; flex-direction: column; height: 100%;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 16px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 8px #22c55e; }
.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 12px; color: #9ca3af; }

.tabs { display: flex; gap: 4px; padding: 0 10px 10px; flex-wrap: wrap; }
.tab {
  flex: 1 1 auto; background: transparent; color: #cbd5e1; border: none;
  padding: 8px 6px; border-radius: 8px; cursor: pointer; font-size: 12.5px; white-space: nowrap;
}
.tab:hover { background: var(--sidebar-hover); }
.tab.active { background: var(--sidebar-active); color: white; }

.sidebar-actions { display: flex; gap: 8px; padding: 6px 12px 10px; }
.search {
  flex: 1; background: #0b1220; border: 1px solid #1f2937; color: #e5e7eb;
  border-radius: 8px; padding: 8px 10px; font-size: 13px;
}
.search::placeholder { color: #6b7280; }
.btn-icon {
  background: var(--accent); color: white; border: none; border-radius: 8px;
  width: 38px; cursor: pointer; font-size: 15px;
}
.btn-icon:hover { filter: brightness(1.1); }

.thread-list { flex: 1; overflow-y: auto; }
.thread {
  display: flex; gap: 10px; align-items: center; padding: 11px 14px; cursor: pointer;
  border-left: 3px solid transparent;
}
.thread:hover { background: var(--sidebar-hover); }
.thread.active { background: #0b1220; border-left-color: var(--accent); }
.thread-avatar {
  width: 38px; height: 38px; min-width: 38px; border-radius: 50%; background: #334155;
  display: flex; align-items: center; justify-content: center; font-weight: 600; color: #e5e7eb; font-size: 14px;
}
.thread-main { flex: 1; min-width: 0; }
.thread-top { display: flex; justify-content: space-between; gap: 8px; }
.thread-name { font-weight: 600; font-size: 13.5px; color: #f1f5f9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-time { font-size: 11px; color: #9ca3af; white-space: nowrap; }
.thread-last { font-size: 12.5px; color: #9ca3af; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.badge {
  background: var(--accent); color: white; border-radius: 999px; font-size: 11px;
  min-width: 18px; height: 18px; padding: 0 5px; display: inline-flex; align-items: center; justify-content: center;
}
.thread-empty { padding: 24px 16px; color: #6b7280; font-size: 13px; text-align: center; }

.conn-footer { padding: 10px 14px; font-size: 11.5px; color: #9ca3af; border-top: 1px solid #1f2937; }
.conn-footer .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.dot.green { background: #22c55e; } .dot.amber { background: #f59e0b; } .dot.red { background: #ef4444; }

/* ── main ───────────────────────────────────────────── */
.main { flex: 1; min-width: 0; background: var(--panel); height: 100%; overflow: hidden; }
.view { display: none; height: 100%; }
.view.active { display: flex; flex-direction: column; }

.panel-scroll { overflow-y: auto; padding: 28px 32px; max-width: 760px; width: 100%; }
.view h1 { font-size: 20px; margin: 0 0 4px; }
.view h2 { font-size: 15px; margin: 0 0 10px; }

/* ── chat ───────────────────────────────────────────── */
.chat-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); gap: 10px;
}
.chat-empty-emoji { font-size: 46px; }
.chat-panel { flex: 1; display: flex; flex-direction: column; height: 100%; }
.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; border-bottom: 1px solid var(--border); background: var(--card);
}
.chat-title { font-weight: 700; font-size: 15px; }
.chat-subtitle { font-size: 12px; color: var(--muted); }
.btn-link { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 12.5px; }

.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 4px; }
.msg-row { display: flex; margin-top: 6px; }
.msg-row.out { justify-content: flex-end; }
/* Width is capped on the row-level wrapper (a stable full-width element), NOT on
   the bubble — capping the bubble with a % of its own shrink-to-fit parent makes
   browsers collapse it to one character per line. */
.msg-content { display: flex; flex-direction: column; max-width: 64%; min-width: 0; }
.msg-row.in  .msg-content { align-items: flex-start; }
.msg-row.out .msg-content { align-items: flex-end; }
.bubble {
  max-width: 100%; padding: 9px 13px; border-radius: 18px; font-size: 14px; line-height: 1.4;
  white-space: pre-wrap; overflow-wrap: break-word;
}
.msg-row.in .bubble { background: var(--in-bubble); color: #111827; border-bottom-left-radius: 5px; }
.msg-row.out .bubble { background: var(--out-bubble); color: white; border-bottom-right-radius: 5px; }
.msg-meta { font-size: 10.5px; color: var(--muted); margin: 2px 4px 0; }
.msg-row.out .msg-meta { text-align: right; }
.msg-meta.failed { color: var(--danger); }
.day-sep { text-align: center; color: var(--muted); font-size: 11px; margin: 14px 0 6px; }

.composer { display: flex; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--border); background: var(--card); }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 20px; padding: 10px 16px;
  font-size: 14px; font-family: inherit; max-height: 140px;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.btn-send {
  background: var(--accent); color: white; border: none; border-radius: 20px; padding: 0 22px;
  font-weight: 600; cursor: pointer;
}
.btn-send:disabled { opacity: .5; cursor: default; }

/* ── forms / buttons ────────────────────────────────── */
.field-label { display: block; font-weight: 600; margin: 18px 0 6px; font-size: 13px; }
.textarea, .input {
  width: 100%; border: 1px solid var(--border); border-radius: 9px; padding: 10px 12px;
  font-size: 14px; font-family: inherit; background: var(--card);
}
.textarea:focus, .input:focus { outline: none; border-color: var(--accent); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.input.grow { flex: 1; }
.hint { font-size: 12px; color: var(--muted); margin-top: 6px; }
.row { display: flex; gap: 10px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.check { font-size: 13px; color: var(--text); display: flex; align-items: center; gap: 6px; }
.file-btn { background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; cursor: pointer; font-size: 13px; }
.file-btn:hover { border-color: var(--accent); }

.btn-primary, .btn-secondary {
  border: none; border-radius: 9px; padding: 10px 18px; font-weight: 600; cursor: pointer; font-size: 13.5px;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:disabled { opacity: .5; cursor: default; }
.btn-secondary { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--accent); }

/* ── bulk preview / results ─────────────────────────── */
.bulk-preview, .bulk-results { margin-top: 16px; background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px; }
.preview-summary { font-weight: 600; margin-bottom: 10px; }
.preview-item { padding: 8px 0; border-bottom: 1px dashed var(--border); font-size: 13px; }
.preview-item:last-child { border-bottom: none; }
.preview-to { color: var(--muted); font-size: 12px; }
.preview-bad { color: var(--danger); }
.result-line { padding: 6px 0; font-size: 13px; display: flex; gap: 8px; align-items: center; }
.result-ok { color: var(--ok); } .result-fail { color: var(--danger); }

/* ── contacts ───────────────────────────────────────── */
.contact-form { margin: 14px 0 20px; }
.contacts-list { display: flex; flex-direction: column; gap: 8px; }
.contact-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; display: flex; justify-content: space-between; align-items: center;
}
.contact-card .c-name { font-weight: 600; }
.contact-card .c-phone { color: var(--muted); font-size: 13px; }
.contact-actions { display: flex; gap: 8px; }

/* ── settings ───────────────────────────────────────── */
.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 18px; margin-top: 16px; }
.settings-status .ok { color: var(--ok); font-weight: 600; }
.settings-status .bad { color: var(--danger); font-weight: 600; }
.settings-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.settings-row:last-child { border-bottom: none; }
.steps { padding-left: 20px; font-size: 13px; line-height: 1.9; }
.webhook-result { margin-top: 12px; font-size: 13px; }

/* ── passcode lock ──────────────────────────────────── */
.lock-screen {
  position: fixed; inset: 0; z-index: 1000; background: var(--sidebar);
  display: flex; align-items: center; justify-content: center;
}
.lock-box { text-align: center; color: #e5e7eb; padding: 24px; }
.lock-emoji { font-size: 40px; margin-bottom: 8px; }
.lock-box h2 { margin: 0 0 22px; font-size: 17px; font-weight: 600; }
.lock-dots { display: flex; gap: 18px; justify-content: center; margin-bottom: 18px; }
.lock-dots span {
  width: 16px; height: 16px; border-radius: 50%;
  border: 1.5px solid #64748b; transition: background .12s;
}
.lock-dots span.filled { background: #e5e7eb; border-color: #e5e7eb; }
/* Invisible but focusable — keeps the numeric keyboard up on mobile. */
#lock-input {
  position: absolute; opacity: 0; height: 1px; width: 1px;
  border: none; padding: 0;
}
.lock-error { color: #f87171; font-size: 13px; min-height: 18px; }
.lock-box.shake { animation: shake .4s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-9px); }
  40%, 80% { transform: translateX(9px); }
}

/* ── mobile nav buttons (hidden on desktop) ─────────── */
.btn-back {
  display: none; background: none; border: none; color: var(--accent);
  font-size: 26px; line-height: 1; cursor: pointer; padding: 0 10px 2px 0;
}
.panel-back { font-size: 15px; font-weight: 600; padding: 0 0 12px; }
.chat-header-titles { flex: 1; min-width: 0; }

/* ── phones ─────────────────────────────────────────── */
@media (max-width: 700px) {
  .app { position: relative; height: 100dvh; }
  .sidebar { width: 100%; min-width: 0; }
  .brand { padding-top: calc(16px + env(safe-area-inset-top)); }
  .main {
    position: fixed; inset: 0; z-index: 20;
    transform: translateX(100%); transition: transform .22s ease;
  }
  body.show-main .main { transform: translateX(0); }
  .btn-back { display: block; }
  .chat-header { padding: calc(10px + env(safe-area-inset-top)) 14px 10px; }
  .panel-scroll {
    padding: calc(16px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
  }
  .composer { padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .conn-footer { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .msg-content { max-width: 82%; }
  .composer textarea, .input, .textarea, .search { font-size: 16px; } /* stop iOS zoom-on-focus */
}

/* ── dialog ─────────────────────────────────────────── */
dialog { border: none; border-radius: 14px; padding: 0; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-form { padding: 22px; width: 340px; display: flex; flex-direction: column; gap: 12px; }
.dialog-form h3 { margin: 0; }
