:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --panel-2: #f7f9fb;
  --border: #e4e8ee;
  --text: #1f2933;
  --muted: #7b8794;
  --accent: #128c7e;      /* WhatsApp green */
  --accent-d: #075e54;
  --in-bubble: #ffffff;
  --out-bubble: #d9fdd3;
  --danger: #d64545;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a; --panel: #111b21; --panel-2: #182229; --border: #222e35;
    --text: #e9edef; --muted: #8696a0; --in-bubble: #202c33; --out-bubble: #005c4b;
    --shadow: 0 1px 3px rgba(0,0,0,.4);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }   /* beat class-level display rules */
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text); display: flex; flex-direction: column;
  font-size: 14px;
}

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 20px; padding: 10px 16px;
  background: var(--accent-d); color: #fff; box-shadow: var(--shadow); z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 16px; }
.brand .logo { font-size: 20px; }
.tenant-picker, .waha-status { display: flex; align-items: center; gap: 8px; }
.tenant-picker label { opacity: .85; }
.topbar input { width: 70px; padding: 5px 8px; border-radius: 6px; border: none; }
.waha-status { margin-left: auto; }
.pill { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill-unknown { background: #64748b; color: #fff; }
.pill-working { background: #22c55e; color: #06301c; }
.pill-scan { background: #f59e0b; color: #3a2500; }
.pill-off { background: #ef4444; color: #fff; }
.conn { font-size: 12px; }
.conn-on { color: #22c55e; } .conn-off { color: #ef4444; }

.btn {
  background: var(--accent); color: #fff; border: none; padding: 6px 12px;
  border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 13px;
}
.btn:hover { filter: brightness(1.06); }
.btn-ghost { background: transparent; color: inherit; border: 1px solid currentColor; opacity: .9; }
.btn-send { align-self: flex-end; }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: 320px 1fr 300px; min-height: 0; }
.col { min-height: 0; display: flex; flex-direction: column; background: var(--panel); }
.col-list { border-right: 1px solid var(--border); }
.col-profile { border-left: 1px solid var(--border); background: var(--panel-2); }

/* Left list */
.list-search { padding: 10px; border-bottom: 1px solid var(--border); }
.list-search input { width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); }
.list-scroll { overflow-y: auto; flex: 1; }
.list-group h3 { margin: 0; padding: 10px 14px 6px; font-size: 12px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted); position: sticky; top: 0; background: var(--panel); }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-item { display: flex; gap: 10px; padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border);
  align-items: center; }
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.ci-avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; flex: none; }
.ci-avatar.group { background: #6366f1; } .ci-avatar.channel { background: #a855f7; }
.ci-body { flex: 1; min-width: 0; }
.ci-top { display: flex; justify-content: space-between; gap: 8px; }
.ci-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { color: var(--muted); font-size: 11px; flex: none; }
.ci-last { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 10px; padding: 1px 6px; border-radius: 4px; margin-left: 6px; vertical-align: middle; }
.badge-waha { background: #dcfce7; color: #166534; } .badge-ezchat { background: #dbeafe; color: #1e40af; }
@media (prefers-color-scheme: dark) {
  .badge-waha { background: #14532d; color: #bbf7d0; } .badge-ezchat { background: #1e3a8a; color: #bfdbfe; }
}

/* Thread */
.col-thread { background: var(--bg); }
.thread-header { padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; min-height: 56px; }
.thread-header .th-title { font-weight: 600; }
.thread-header .th-sub { color: var(--muted); font-size: 12px; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 68%; padding: 7px 11px; border-radius: 10px; box-shadow: var(--shadow); position: relative;
  word-wrap: break-word; white-space: pre-wrap; }
.msg.in { align-self: flex-start; background: var(--in-bubble); border-top-left-radius: 3px; }
.msg.out { align-self: flex-end; background: var(--out-bubble); border-top-right-radius: 3px; }
.msg .sender { font-size: 11px; font-weight: 700; color: var(--accent); margin-bottom: 2px; }
.msg .meta { font-size: 10px; color: var(--muted); text-align: right; margin-top: 3px; }
.msg .mtype { font-style: italic; color: var(--muted); }
.day-sep { align-self: center; background: var(--panel); color: var(--muted); font-size: 11px;
  padding: 3px 10px; border-radius: 8px; margin: 8px 0; }
.composer { display: flex; gap: 10px; padding: 12px 16px; background: var(--panel); border-top: 1px solid var(--border); }
.composer textarea { flex: 1; resize: none; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font: inherit; max-height: 120px; }

/* Profile */
.profile-empty, .thread-header.empty .th-title { color: var(--muted); }
.profile-empty { padding: 20px; text-align: center; margin-top: 30px; }
.profile { padding: 18px; overflow-y: auto; }
.avatar { width: 64px; height: 64px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 26px; font-weight: 700; margin: 0 auto 10px; }
.p-name { text-align: center; font-weight: 600; font-size: 16px; }
.p-meta { text-align: center; color: var(--muted); font-size: 12px; margin-bottom: 16px; word-break: break-all; }
.p-section { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 14px; }
.p-section > label { display: block; font-weight: 600; font-size: 12px; margin-bottom: 8px; }
.p-section .hint { font-weight: 400; color: var(--muted); }
.assign-row { display: flex; gap: 6px; }
.assign-row select { flex: 1; padding: 6px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); }
.notes { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.note { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.note .n-meta { color: var(--muted); font-size: 11px; margin-top: 4px; }
.note-form { display: flex; flex-direction: column; gap: 6px; }
.note-form textarea { padding: 8px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font: inherit; resize: vertical; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: grid; place-items: center; z-index: 20; }
.modal-card { background: var(--panel); border-radius: 12px; padding: 24px; width: 340px; text-align: center; position: relative; }
.modal-card h3 { margin: 0 0 6px; }
.modal-close { position: absolute; top: 10px; right: 14px; background: none; border: none; font-size: 22px;
  cursor: pointer; color: var(--muted); }
.qr-box { margin: 14px auto; width: 240px; height: 240px; display: grid; place-items: center;
  background: #fff; border-radius: 8px; }

/* Settings modal */
.settings-card { width: 460px; max-width: 94vw; max-height: 88vh; overflow-y: auto; text-align: left; }
.settings-card h3 { text-align: center; }
.cfg-section { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 14px; }
.cfg-section h4 { margin: 0 0 10px; font-size: 14px; }
.cfg-section label { display: block; font-size: 12px; font-weight: 600; margin: 8px 0 4px; }
.cfg-section input { width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font: inherit; }
.cfg-row { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.cfg-status { font-size: 12px; color: #16a34a; }
.hook-label { margin-top: 12px; }
.hook-box { display: flex; align-items: center; gap: 6px; background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; }
.hook-box code { flex: 1; font-size: 11px; word-break: break-all; color: var(--text); }
.hook-box .copy { padding: 3px 8px; font-size: 12px; }
.qr-box img { width: 100%; height: 100%; object-fit: contain; }
.hint { color: var(--muted); font-size: 12px; }

/* whoami / logout */
.whoami { display: flex; align-items: center; gap: 8px; font-size: 13px; }

/* Login overlay */
.login-overlay { position: fixed; inset: 0; background: var(--bg); display: grid; place-items: center; z-index: 40; }
.login-card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px 26px; width: 340px; display: flex; flex-direction: column; gap: 8px; box-shadow: var(--shadow); }
.login-brand { font-size: 18px; font-weight: 700; text-align: center; margin-bottom: 12px; }
.login-brand .logo { font-size: 22px; }
.login-card label { font-size: 12px; font-weight: 600; margin-top: 6px; }
.login-card input { padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font: inherit; }
.login-btn { margin-top: 14px; padding: 10px; font-size: 15px; }
.login-error { background: #fde8e8; color: var(--danger); padding: 8px 10px; border-radius: 8px; font-size: 13px; }
@media (prefers-color-scheme: dark) { .login-error { background: #3b1a1a; } }

/* Toast */
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #1f2933; color: #fff; padding: 10px 16px; border-radius: 8px; z-index: 30; box-shadow: var(--shadow); }
.toast.err { background: var(--danger); }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .col-profile { display: none; }
}
