:root {
  --bg: #f6f6f3;
  --surface: #ffffff;
  --line: #e3e2dc;
  --text: #1d1d1b;
  --muted: #6d6c66;
  --accent: #1f7a55;
  --accent-ink: #ffffff;
  --accent-soft: #e3f1ea;
  --danger: #b3261e;
  --warn: #9a6200;
  --bubble-out: #dcf3e5;
  --radius: 8px;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--text);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #1f1f1d;
    --line: #34332f;
    --text: #ecebe6;
    --muted: #9c9b94;
    --accent: #3fae80;
    --accent-ink: #0d1f17;
    --accent-soft: #1d3329;
    --danger: #f2867d;
    --warn: #e0a84a;
    --bubble-out: #1f3a2c;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
[hidden] { display: none !important; }
h1, h2 { margin: 0 0 12px; font-weight: 600; }
h1 { font-size: 20px; } h2 { font-size: 16px; }
code { background: var(--accent-soft); padding: 1px 4px; border-radius: 4px; font-size: 12px; }
a { color: var(--accent); }
.muted { color: var(--muted); } .small { font-size: 12px; } .error { color: var(--danger); }

button {
  font: inherit; cursor: pointer; border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
  padding: 7px 14px; border-radius: var(--radius); font-weight: 500;
}
button:disabled { opacity: .5; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--text); border-color: var(--line); }
button.ghost:hover { border-color: var(--muted); }
button.danger { color: var(--danger); }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 7px 10px; width: 100%;
}
textarea { resize: vertical; }
label { display: grid; gap: 4px; font-weight: 500; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 0; display: grid; gap: 10px; }
legend { font-weight: 600; padding: 0 4px; }
.row { display: flex; gap: 10px; align-items: end; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }

.top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 20px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-size: 15px; }
.tabs { display: flex; gap: 2px; flex: 1; overflow-x: auto; }
.tabs button { background: none; border: 0; color: var(--muted); padding: 8px 12px; border-radius: 6px; white-space: nowrap; }
.tabs button.active { color: var(--text); background: var(--bg); }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 10px; padding: 0 6px; font-size: 11px; margin-left: 4px; }
.wa-pill { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.wa-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.wa-pill.ok .dot { background: var(--accent); }
.wa-pill.bad .dot { background: var(--danger); }
.wa-pill button { padding: 3px 8px; font-size: 12px; }

main { padding: 20px; max-width: 1200px; margin: 0 auto; }
.panel { display: none; } .panel.active { display: block; }
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar input[type=search] { max-width: 280px; } .toolbar select { width: auto; }
.bulkbar { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; padding: 6px 10px; background: var(--accent-soft); border-radius: var(--radius); }

.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-weight: 600; color: var(--muted); font-size: 12px; position: sticky; top: 0; background: var(--surface); }
tr:last-child td { border-bottom: 0; }
td.chk, th.chk { width: 32px; } td.chk input, th.chk input { width: auto; }
td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
td .ghost { padding: 3px 8px; font-size: 12px; }
.tag { display: inline-block; background: var(--accent-soft); color: var(--text); border-radius: 4px; padding: 0 6px; margin: 0 4px 2px 0; font-size: 12px; }
.empty { text-align: center; color: var(--muted); padding: 32px; margin: 0; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.col { display: grid; gap: 14px; align-content: start; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips label { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; border: 1px solid var(--line); border-radius: 20px; padding: 4px 10px; cursor: pointer; background: var(--surface); }
.chips input { width: auto; margin: 0; }
.chips label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.preview { background: var(--bg); border: 1px dashed var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 6px; }
.bubble { max-width: 80%; padding: 7px 10px; border-radius: 8px; background: var(--surface); white-space: pre-wrap; word-wrap: break-word; border: 1px solid var(--line); }
.bubble.out { background: var(--bubble-out); justify-self: end; border-color: transparent; }
.bubble .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.bubble a { color: var(--accent); }

.cards { display: grid; gap: 10px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 8px; }
.card-head { display: flex; gap: 10px; align-items: center; }
.card-head h2 { margin: 0; flex: 1; cursor: pointer; }
.status { font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); }
.status.running { color: var(--accent); border-color: var(--accent); }
.status.failed, .status.cancelled { color: var(--danger); }
.status.sent, .status.done { color: var(--accent); }
.bar { height: 6px; border-radius: 3px; background: var(--bg); overflow: hidden; display: flex; }
.bar .s { background: var(--accent); } .bar .f { background: var(--danger); }

.inbox.active { display: grid; grid-template-columns: 300px 1fr; height: calc(100vh - 100px); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.convs { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.convs-head { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); }
.convs ul { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.convs li { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.convs li.active, .convs li:hover { background: var(--bg); }
.convs .n { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convs .p { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convs .t { color: var(--muted); font-size: 11px; text-align: right; }
.chat { display: flex; flex-direction: column; min-height: 0; }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--line); font-weight: 600; }
.chat-body { flex: 1; overflow-y: auto; padding: 16px; display: grid; gap: 6px; align-content: start; background: var(--bg); }
.reply { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); align-items: end; }
.reply textarea { max-height: 140px; }
.attach { cursor: pointer; font-size: 18px; padding: 6px; display: block; }
.reply-file { padding: 0 12px 8px; margin: 0; }

.login { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 28px; max-width: 380px; width: 100%; }
.qr-box { width: 280px; height: 280px; margin: 0 auto 16px; display: grid; place-items: center; background: #fff; border-radius: 8px; }
.qr-box img { width: 280px; height: 280px; }
.steps { padding-left: 20px; margin: 0; color: var(--muted); }

dialog { border: 1px solid var(--line); border-radius: 12px; padding: 20px; background: var(--surface); color: var(--text); width: min(440px, calc(100vw - 32px)); }
dialog.wide { width: min(900px, calc(100vw - 32px)); max-height: 85vh; }
dialog form { display: grid; gap: 12px; }
dialog::backdrop { background: rgb(0 0 0 / .35); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-head h2 { margin: 0; }
#cdSummary { margin: 8px 0 12px; }

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

@media (max-width: 760px) {
  .top { flex-wrap: wrap; gap: 8px; padding: 10px 16px; }
  .tabs { order: 3; flex-basis: 100%; }
  main { padding: 16px; }
  .grid2 { grid-template-columns: 1fr; }
  .inbox.active { grid-template-columns: 1fr; height: auto; }
  .convs { border-right: 0; border-bottom: 1px solid var(--line); max-height: 40vh; }
  .chat-body { min-height: 50vh; }
}

/* Inbox filter */
.convs-filter { display: grid; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.convs-filter input, .convs-filter select { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); width: 100%; min-width: 0; }
.seg { display: flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { flex: 1; background: transparent; color: var(--text); border: 0; border-radius: 0; padding: 5px 4px; font-size: 12px; font-weight: 500; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.convs-filter p { margin: 0; }
.convs .tags { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 4px; }
.convs .tag { font-size: 10px; padding: 0 6px; border-radius: 4px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.convs li.empty { display: block; cursor: default; text-align: center; padding: 18px 12px; }
.convs li.empty:hover { background: transparent; }
button.link { background: none; border: 0; color: var(--accent); padding: 0; font-weight: 600; text-decoration: underline; }

.convs .tag.more { background: transparent; border: 1px solid var(--line); color: var(--muted); cursor: help; }
.head-tags { display: inline-flex; flex-wrap: wrap; gap: 4px; margin-left: 8px; vertical-align: middle; }
.head-tags .tag { font-size: 11px; padding: 0 6px; border-radius: 4px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.sync-row { display: flex; align-items: center; gap: 8px; }
.sync-row button { padding: 4px 10px; font-size: 12px; }
