:root {
  --head: #f0f2f5;
  --panel: #ffffff;
  --border: #e9edef;
  --text: #111b21;
  --muted: #667781;
  --accent: #00a884;
  --badge: #25d366;
  --tick: #53bdeb;
  --chat-bg: #efeae2;
  --doodle: rgba(17, 27, 33, .05);
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --hover: #f5f6f6;
  --active: #f0f2f5;
  --notice: #d9fdff;
  --shadow: 0 1px .5px rgba(11, 20, 26, .13);
}
:root[data-theme="dark"] {
  --head: #202c33;
  --panel: #111b21;
  --border: #222d34;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --badge: #00a884;
  --tick: #53bdeb;
  --chat-bg: #0b141a;
  --doodle: rgba(233, 237, 239, .04);
  --bubble-in: #202c33;
  --bubble-out: #005c4b;
  --hover: #202c33;
  --active: #2a3942;
  --notice: #182229;
  --shadow: 0 1px .5px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--panel); color: var(--text);
  font: 15px/1.4 "Segoe UI", Helvetica, -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
}
button { font: inherit; }

.app { display: flex; height: 100vh; overflow: hidden; }

/* ---------- shared chrome ---------- */
.pane-head {
  display: flex; align-items: center; gap: 12px; flex: none;
  height: 60px; padding: 0 16px; background: var(--head); border-bottom: 1px solid var(--border);
}
.head-icons { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: rgba(134, 150, 160, .18); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #f0b232; margin: 0 8px; }
.status-dot.online { background: var(--badge); }
.status-dot.offline { background: #e8574a; }

.avatar {
  width: 40px; height: 40px; flex: none; border-radius: 50%;
  display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 16px;
  background: #6a7b8a; user-select: none;
}
.avatar-me { background: var(--accent); }

/* ---------- left pane ---------- */
.pane-left {
  width: 30%; min-width: 300px; max-width: 420px; flex: none;
  display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--border);
}
.notice {
  display: flex; align-items: flex-start; gap: 12px; padding: 14px 16px;
  background: var(--notice); border-bottom: 1px solid var(--border);
}
.notice-icon { width: 34px; height: 34px; flex: none; border-radius: 50%; background: var(--accent); display: grid; place-items: center; }
.notice-icon svg { width: 20px; height: 20px; fill: #fff; }
.notice-body { flex: 1; min-width: 0; }
.notice-body strong { display: block; font-size: 15px; }
.notice-body p { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
.notice-close { width: 28px; height: 28px; }
.notice-close svg { width: 16px; height: 16px; }

.search-row { position: relative; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.search-row input {
  width: 100%; height: 36px; padding: 0 12px 0 62px; border: 0; border-radius: 8px;
  background: var(--head); color: var(--text); outline: none; font-size: 14px;
}
.search-icon { position: absolute; left: 34px; top: 50%; transform: translateY(-50%); color: var(--muted); display: grid; }
.search-icon svg { width: 18px; height: 18px; fill: currentColor; }

.chat-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; }
.chat-list li { display: flex; gap: 14px; align-items: center; padding: 10px 16px; cursor: pointer; }
.chat-list li + li { box-shadow: inset 0 1px 0 var(--border); }
.chat-list li:hover { background: var(--hover); }
.chat-list li.active { background: var(--active); }
.row-body { flex: 1; min-width: 0; }
.row-top { display: flex; align-items: baseline; gap: 8px; }
.row-name { flex: 1; min-width: 0; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-time { font-size: 12px; color: var(--muted); flex: none; }
.row-time.unread { color: var(--badge); }
.row-bottom { display: flex; align-items: center; gap: 6px; margin-top: 2px; }
.row-snippet {
  flex: 1; min-width: 0; font-size: 14px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.badge {
  flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--badge); color: #fff; font-size: 12px; font-weight: 600;
  display: grid; place-items: center;
}
.ticks { flex: none; display: inline-grid; color: var(--muted); }
.ticks svg { width: 16px; height: 16px; fill: currentColor; }
.ticks.read { color: var(--tick); }

/* ---------- right pane ---------- */
.pane-right { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.no-chat {
  flex: 1; display: grid; place-items: center; text-align: center;
  background: var(--head); border-bottom: 6px solid var(--accent);
}
.no-chat h2 { font-weight: 400; font-size: 30px; margin: 0 0 12px; color: var(--text); }
.no-chat p { margin: 0; max-width: 460px; color: var(--muted); font-size: 14px; }
.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head-text { min-width: 0; }
.chat-name { font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { font-size: 13px; color: var(--muted); }
.chat-search { padding: 8px 16px; background: var(--head); border-bottom: 1px solid var(--border); }
.chat-search input {
  width: 100%; height: 34px; padding: 0 12px; border: 0; border-radius: 8px;
  background: var(--panel); color: var(--text); outline: none; font-size: 14px;
}

.messages {
  flex: 1; overflow-y: auto; padding: 18px 6%;
  display: flex; flex-direction: column; gap: 3px;
  background-color: var(--chat-bg);
  background-image:
    radial-gradient(var(--doodle) 1.4px, transparent 1.4px),
    radial-gradient(var(--doodle) 1.4px, transparent 1.4px);
  background-size: 42px 42px; background-position: 0 0, 21px 21px;
}
.system-note, .divider {
  align-self: center; margin: 10px 0; padding: 6px 12px; border-radius: 8px;
  background: var(--bubble-in); color: var(--muted); font-size: 12.5px; text-align: center;
  box-shadow: var(--shadow); max-width: 80%;
}
.divider { text-transform: uppercase; letter-spacing: .3px; font-weight: 500; }
.empty-chat { margin: auto; color: var(--muted); font-size: 14px; }

.bubble {
  position: relative; max-width: 65%; padding: 6px 9px 8px; border-radius: 8px;
  background: var(--bubble-in); box-shadow: var(--shadow);
  align-self: flex-start; word-wrap: break-word;
}
.bubble.out { background: var(--bubble-out); align-self: flex-end; }
.bubble.tail-in { border-top-left-radius: 0; margin-top: 8px; }
.bubble.tail-out { border-top-right-radius: 0; margin-top: 8px; }
.bubble.tail-in::before, .bubble.tail-out::before {
  content: ""; position: absolute; top: 0; width: 8px; height: 13px;
}
.bubble.tail-in::before { left: -8px; background: var(--bubble-in); clip-path: polygon(100% 0, 0 0, 100% 100%); }
.bubble.tail-out::before { right: -8px; background: var(--bubble-out); clip-path: polygon(0 0, 100% 0, 0 100%); }
.bubble .who { font-size: 12.5px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.bubble .body { font-size: 14.2px; }
.bubble .media { font-style: italic; color: var(--muted); }
.bubble .row { display: flex; align-items: flex-end; gap: 8px; }
.bubble .body { flex: 1; min-width: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble .meta {
  flex: none; display: inline-flex; align-items: center; gap: 2px;
  margin-bottom: -2px; font-size: 11px; color: var(--muted); white-space: nowrap;
}
.bubble .meta .ticks svg { width: 15px; height: 15px; }
.bubble mark { background: #f7e08a; color: inherit; border-radius: 2px; }

.scroll-bottom {
  position: absolute; right: 26px; bottom: 86px; width: 42px; height: 42px;
  border: 0; border-radius: 50%; background: var(--panel); color: var(--muted);
  box-shadow: 0 1px 3px rgba(11, 20, 26, .28); cursor: pointer; display: grid; place-items: center;
}
.scroll-bottom svg { width: 22px; height: 22px; fill: currentColor; }

.composer { display: flex; align-items: center; gap: 8px; flex: none; padding: 8px 16px; background: var(--head); }
.composer input {
  flex: 1; height: 42px; padding: 0 16px; border: 0; border-radius: 8px;
  background: var(--panel); color: var(--text); outline: none; font-size: 15px;
}
.composer .send-btn { color: var(--muted); }
.composer .send-btn:hover { color: var(--accent); }

@media (max-width: 780px) {
  .pane-left { width: 100%; min-width: 0; max-width: none; }
  .pane-left.hidden-mobile { display: none; }
  .pane-right { display: none; }
  .pane-right.show-mobile { display: flex; }
  .messages { padding: 14px 12px; }
  .bubble { max-width: 82%; }
}

/* ---------- inline keyboard in bubbles ---------- */
.bubble .keyboard { margin: 6px -9px -8px; border-top: 1px solid rgba(0, 0, 0, .08); }
:root[data-theme="dark"] .bubble .keyboard { border-top-color: rgba(255, 255, 255, .12); }
.bubble .kb-btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  width: 100%; padding: 9px 8px; border: 0; background: transparent;
  color: #027eb5; font-size: 14px; font-weight: 500; cursor: default; text-decoration: none;
}
:root[data-theme="dark"] .bubble .kb-btn { color: #53bdeb; }
.bubble .kb-btn + .kb-btn { border-top: 1px solid rgba(0, 0, 0, .08); }
:root[data-theme="dark"] .bubble .kb-btn + .kb-btn { border-top-color: rgba(255, 255, 255, .12); }
a.kb-btn:hover { text-decoration: underline; cursor: pointer; }
.bubble .kb-btn svg { width: 14px; height: 14px; fill: currentColor; opacity: .75; }

.click-chip {
  align-self: center; margin: 6px 0; padding: 5px 12px; border-radius: 12px;
  background: var(--bubble-out); color: var(--muted); font-size: 12.5px;
  box-shadow: var(--shadow); max-width: 80%; text-align: center;
}
.click-chip b { color: var(--text); }

/* ---------- button composer ---------- */
.button-panel {
  flex: none; padding: 12px 16px; background: var(--head);
  border-top: 1px solid var(--border); max-height: 46vh; overflow-y: auto;
}
.bp-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.bp-head strong { font-size: 14px; }
.button-panel textarea, .bp-row input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); color: var(--text); font: inherit; font-size: 14px; outline: none; resize: vertical;
}
.button-panel textarea:focus, .bp-row input:focus { border-color: var(--accent); }
.bp-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.bp-row .bp-label { flex: 1 1 40%; }
.bp-row .bp-url { flex: 1 1 60%; }
.bp-row .icon-btn { width: 32px; height: 32px; }
.bp-row .icon-btn svg { width: 16px; height: 16px; }
.bp-foot { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.bp-add, .bp-send { border: 0; border-radius: 8px; padding: 9px 16px; font-size: 14px; cursor: pointer; }
.bp-add { background: transparent; color: var(--accent); font-weight: 600; }
.bp-add:hover { background: rgba(0, 168, 132, .12); }
.bp-send { margin-left: auto; background: var(--accent); color: #fff; font-weight: 600; }
.bp-send:disabled { opacity: .55; cursor: default; }
.bp-hint { margin: 8px 0 0; font-size: 12px; color: var(--muted); }

/* ---------- settings ---------- */
.settings-view { flex: 1; display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.settings-body { flex: 1; overflow-y: auto; padding: 20px 24px 40px; }
.settings-body h3 { font-size: 15px; margin: 0; }
.rules-head { display: flex; align-items: center; justify-content: space-between; margin: 24px 0 12px; }
.rules-list { display: flex; flex-direction: column; gap: 14px; }

.rule-card { border: 1px solid var(--border); border-radius: 10px; background: var(--panel); overflow: hidden; }
.rule-card.disabled { opacity: .62; }
.rule-top {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--head); cursor: pointer;
}
.rule-name { font-weight: 600; font-size: 15px; }
.rule-trigger { color: var(--muted); font-size: 13px; margin-top: 2px; }
.rule-trigger code { background: rgba(0, 168, 132, .14); color: var(--accent); padding: 1px 6px; border-radius: 4px; font-size: 12.5px; }
.rule-top .grow { flex: 1; min-width: 0; }
.rule-body { padding: 14px; display: none; flex-direction: column; gap: 12px; }
.rule-card.open .rule-body { display: flex; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.field input, .field textarea, .field select {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); color: var(--text); font: inherit; font-size: 14px; outline: none; resize: vertical;
}
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }
.field .hint { font-size: 12px; color: var(--muted); font-weight: 400; text-transform: none; letter-spacing: 0; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.rule-actions { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.rule-actions .spacer { flex: 1; }
.btn-danger { border: 0; border-radius: 8px; padding: 9px 14px; background: transparent; color: #e8574a; font-weight: 600; cursor: pointer; }
.btn-danger:hover { background: rgba(232, 87, 74, .12); }
.btn-ghost { border: 1px solid var(--border); border-radius: 8px; padding: 8px 14px; background: transparent; color: var(--text); cursor: pointer; }

.switch { position: relative; width: 38px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch .track {
  position: absolute; inset: 0; border-radius: 11px; background: #b9c4cc; transition: background .15s; pointer-events: none;
}
.switch .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; transition: transform .15s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }

.tester { padding: 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--head); }
.tester label { display: block; font-size: 12.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; margin-bottom: 8px; }
.tester-row { display: flex; gap: 10px; }
.tester-row input {
  flex: 1; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); color: var(--text); font-size: 14px; outline: none;
}
.tester-row button { border: 0; border-radius: 8px; padding: 9px 18px; background: var(--accent); color: #fff; font-weight: 600; cursor: pointer; }
.test-result { margin-top: 10px; padding: 10px 12px; border-radius: 8px; font-size: 13.5px; }
.test-result.hit { background: rgba(0, 168, 132, .13); color: var(--text); }
.test-result.miss { background: rgba(232, 87, 74, .13); color: var(--text); }
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #111b21; color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 14px;
  box-shadow: 0 3px 12px rgba(0, 0, 0, .3); z-index: 20;
}
