:root {
  --bg: #f4f5f2;
  --card: #ffffff;
  --ink: #1c2420;
  --muted: #6b746f;
  --line: #e2e5e0;
  --green: #15803d;
  --green-soft: #e6f4ea;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --red: #b91c1c;
  --red-soft: #fde8e8;
  --blue-soft: #e8f0fe;
  --chat-bg: #efeae2;
  --bubble: #d9fdd3;
  --radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  font-size: 15px;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
[hidden] { display: none !important; }
h1, h2 { margin: 0 0 12px; }
h2 { font-size: 16px; }
a { color: var(--green); }
.muted { color: var(--muted); }
.center { text-align: center; }
.error { color: var(--red); min-height: 1.2em; margin: 8px 0 0; }
.hint { color: var(--muted); font-size: 13px; line-height: 1.45; }

input, textarea, select {
  font: inherit;
  color: inherit;
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
input:focus, textarea:focus { outline: 2px solid #86c79a; border-color: var(--green); }
textarea { resize: vertical; line-height: 1.45; }

.btn {
  font: inherit;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: #c5cbc4; }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #116a32; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: wait; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--card); padding: 32px; border-radius: 16px; width: min(360px, 100%); display: grid; gap: 12px; text-align: center; box-shadow: 0 4px 24px rgba(0,0,0,.06); }
.logo { font-size: 40px; }

/* header */
.top {
  background: var(--card);
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas: "brand pills" "tabs tabs";
  gap: 10px 16px;
  padding: 12px 20px 0;
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { grid-area: brand; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.pills { grid-area: pills; display: flex; gap: 8px; justify-content: flex-end; align-items: center; flex-wrap: wrap; }
.pill { font-size: 12px; padding: 4px 10px; border-radius: 99px; background: var(--line); color: var(--muted); }
.pill::before { content: "●"; margin-right: 5px; }
.pill.ok { background: var(--green-soft); color: var(--green); }
.pill.warn { background: var(--amber-soft); color: var(--amber); }
.pill.bad { background: var(--red-soft); color: var(--red); }
.tabs { grid-area: tabs; display: flex; gap: 4px; overflow-x: auto; }
.tabs button {
  font: inherit; background: none; border: 0; padding: 10px 14px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent;
}
.tabs button.active { color: var(--ink); border-color: var(--green); font-weight: 600; }
.count { background: var(--amber); color: #fff; border-radius: 99px; padding: 0 7px; font-size: 12px; margin-left: 4px; }

/* switch */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; font-size: 13px; }
.switch input { display: none; }
.switch span { width: 38px; height: 22px; border-radius: 99px; background: #cfd4ce; position: relative; transition: .2s; flex: none; }
.switch span::after { content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%; background: #fff; top: 2px; left: 2px; transition: .2s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked + span { background: var(--green); }
.switch input:checked + span::after { left: 18px; }

.banner { margin: 12px 20px 0; padding: 10px 14px; border-radius: 10px; font-size: 14px; }
.banner.warn { background: var(--amber-soft); color: #78350f; }
.banner a { color: inherit; font-weight: 600; cursor: pointer; }

/* layout */
.tab { padding: 20px; max-width: 1280px; margin: 0 auto; }
#tab-messages { display: grid; grid-template-columns: 240px minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }

/* project list */
.projects { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; position: sticky; top: 110px; }
.projects-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.projects-head h2 { margin: 0; }
#projectList { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
#projectList li {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; gap: 8px; align-items: center;
}
#projectList li:hover { background: var(--bg); }
#projectList li.active { background: var(--green-soft); font-weight: 600; }
#projectList li .n { font-size: 12px; color: var(--muted); font-weight: 400; }
#projectList li.off .t { color: var(--muted); text-decoration: line-through; }
#projectList li.def { border-bottom: 1px dashed var(--line); border-radius: 8px 8px 0 0; margin-bottom: 4px; }

/* editor */
.editor { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
.editor-head { display: flex; gap: 16px; align-items: flex-end; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field.grow { flex: 1; }
.field label { font-size: 13px; font-weight: 600; }
.default-note { background: var(--blue-soft); padding: 10px 12px; border-radius: 8px; font-size: 14px; margin: 0 0 14px; }
.vars { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 13px; margin-bottom: 12px; }
.chip { font: inherit; font-size: 12px; font-family: ui-monospace, Menlo, Consolas, monospace; background: var(--green-soft); color: var(--green); border: 0; padding: 3px 8px; border-radius: 6px; cursor: pointer; }

.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.step { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #fbfcfa; }
.step-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.step-head b { font-size: 13px; }
.step-tools { display: flex; gap: 2px; }
.icon-btn { font: inherit; background: none; border: 0; cursor: pointer; padding: 4px 7px; border-radius: 6px; color: var(--muted); }
.icon-btn:hover { background: var(--line); color: var(--ink); }
.icon-btn:disabled { opacity: .3; cursor: default; background: none; }
.step textarea { min-height: 90px; background: #fff; }
.attach { display: flex; align-items: center; gap: 10px; margin-top: 8px; flex-wrap: wrap; font-size: 13px; }
.attach img { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }
.attach .pdf { display: inline-flex; align-items: center; gap: 6px; background: var(--red-soft); color: var(--red); padding: 6px 10px; border-radius: 6px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach label.btn { font-size: 13px; padding: 5px 10px; }
.attach input[type=file] { display: none; }
.add-step { width: 100%; margin-top: 12px; border-style: dashed; color: var(--green); }

.editor-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); gap: 10px; flex-wrap: wrap; }
.foot-left, .foot-right { display: flex; gap: 8px; align-items: center; }
.foot-right { margin-left: auto; }
.dirty { color: var(--amber); font-size: 13px; }
.test-box { margin-top: 16px; background: var(--bg); padding: 12px; border-radius: 10px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 14px; }
.test-box input { width: 200px; }

/* WhatsApp preview */
.preview { position: sticky; top: 110px; }
.phone { border-radius: 22px; overflow: hidden; border: 6px solid #1f2722; background: var(--chat-bg); box-shadow: 0 8px 30px rgba(0,0,0,.12); }
.phone-bar { background: #075e54; color: #fff; padding: 12px 14px; display: flex; gap: 10px; align-items: center; font-weight: 600; font-size: 14px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: #cfd8dc; color: #37474f; display: grid; place-items: center; font-size: 13px; }
.chat { padding: 12px 10px; min-height: 420px; max-height: 70vh; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.bubble { background: var(--bubble); border-radius: 8px 0 8px 8px; padding: 6px 8px 4px; max-width: 88%; font-size: 13.5px; line-height: 1.38; white-space: pre-wrap; word-wrap: break-word; box-shadow: 0 1px 1px rgba(0,0,0,.08); }
.bubble img { display: block; width: 100%; border-radius: 6px; margin-bottom: 4px; }
.bubble .doc { display: flex; gap: 8px; align-items: center; background: rgba(0,0,0,.05); border-radius: 6px; padding: 8px; margin-bottom: 4px; font-size: 13px; }
.bubble .time { display: block; text-align: right; font-size: 10.5px; color: #667781; margin-top: 2px; }
.chat .empty { align-self: center; color: var(--muted); font-size: 13px; margin-top: 40px; }

/* leads */
.stats { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.stat { background: var(--bg); border-radius: 10px; padding: 10px 16px; }
.stat b { font-size: 22px; display: block; }
.stat span { font-size: 12px; color: var(--muted); }
.manual summary { cursor: pointer; font-weight: 600; color: var(--green); }
.manual textarea { margin: 8px 0; }
.log { width: 100%; border-collapse: collapse; font-size: 14px; }
.log th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line); }
.log td { padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.log td small { color: var(--muted); display: block; }
.status { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 99px; display: inline-block; }
.status.sent { background: var(--green-soft); color: var(--green); }
.status.queued, .status.sending { background: var(--amber-soft); color: var(--amber); }
.status.failed { background: var(--red-soft); color: var(--red); }
.status.skipped { background: var(--line); color: var(--muted); }

/* connections & settings */
.conn { max-width: 720px; }
.qr { display: flex; gap: 24px; align-items: center; flex-wrap: wrap; margin: 12px 0; }
.qr img { width: 260px; height: 260px; border: 1px solid var(--line); border-radius: 8px; image-rendering: pixelated; }
.qr ol { line-height: 1.8; padding-left: 20px; }
.stack { display: grid; gap: 10px; margin-top: 12px; }
.stack p { margin: 0; line-height: 1.5; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row input { flex: 1; min-width: 180px; }
.settings { max-width: 860px; }
.settings h2 { margin-top: 8px; padding-top: 8px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px 20px; margin-bottom: 18px; }
.grid label { display: grid; gap: 6px; font-size: 13px; font-weight: 600; align-content: start; }
.grid label small { font-weight: 400; line-height: 1.4; }
.grid label.check { display: flex; gap: 8px; align-items: center; font-weight: 400; font-size: 14px; }
.grid label.check input { width: auto; }
.pair { display: flex; gap: 8px; align-items: center; font-weight: 400; }
.pair input { width: 90px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 20; font-size: 14px; max-width: calc(100% - 32px); }
.toast.bad { background: var(--red); }

@media (max-width: 1100px) {
  #tab-messages { grid-template-columns: 220px minmax(0, 1fr); }
  .preview { grid-column: 2; position: static; }
}
@media (max-width: 760px) {
  .tab { padding: 16px; }
  .top { padding: 10px 16px 0; grid-template-areas: "brand" "pills" "tabs"; grid-template-columns: 1fr; }
  .pills { justify-content: flex-start; }
  #tab-messages { grid-template-columns: 1fr; }
  .projects, .preview { position: static; grid-column: auto; }
  .log thead { display: none; }
  .log tr { display: grid; grid-template-columns: 1fr auto; border-bottom: 1px solid var(--line); padding: 8px 0; }
  .log td { border: 0; padding: 4px 0; }
  .banner { margin: 12px 16px 0; }
}
