:root {
  --bg: #ffffff;
  --bg-2: #f4f4f5;
  --chat-bg: #c8d7b8;
  --text: #000000;
  --hint: #707579;
  --link: #3390ec;
  --accent: #3390ec;
  --accent-text: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #e3fee0;
  --bubble-out-meta: #4fae4e;
  --border: #dadce0;
  --hover: #f1f1f1;
  --active: #3390ec;
  --danger: #e53935;
  --shadow: 0 1px 2px rgba(16, 35, 47, 0.15);
  --radius: 12px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #212121;
  --bg-2: #181818;
  --chat-bg: #0f0f0f;
  --text: #ffffff;
  --hint: #aaaaaa;
  --link: #8774e1;
  --accent: #8774e1;
  --accent-text: #ffffff;
  --bubble-in: #212121;
  --bubble-out: #766ac8;
  --bubble-out-meta: #d6d1ff;
  --border: #303030;
  --hover: #2c2c2c;
  --active: #8774e1;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg-2);
  color: var(--text);
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }
a { color: var(--link); }
.hidden { display: none !important; }

.icon-btn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 50%;
  font-size: 20px; color: var(--hint); flex: none;
}
.icon-btn:hover { background: var(--hover); }
.primary {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px;
  padding: 12px; font-weight: 600;
}

/* ---------- auth ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 360px; background: var(--bg); border-radius: 16px; padding: 32px 24px;
  box-shadow: var(--shadow); text-align: center;
}
.logo {
  width: 96px; height: 96px; margin: 0 auto 12px; border-radius: 50%; background: var(--accent);
  color: #fff; display: grid; place-items: center; font-size: 48px;
}
.auth h1 { margin: 0 0 16px; font-size: 22px; }
.tabs { display: flex; gap: 4px; margin-bottom: 16px; background: var(--bg-2); padding: 4px; border-radius: 10px; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 8px; }
.tabs button.active { background: var(--bg); box-shadow: var(--shadow); font-weight: 600; }
#auth-form { display: flex; flex-direction: column; gap: 10px; }
#auth-form input {
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); outline: none;
}
#auth-form input:focus { border-color: var(--accent); }
.error { color: var(--danger); min-height: 1em; margin: 4px 0 0; font-size: 14px; }

/* ---------- layout ---------- */
.app { display: flex; height: 100vh; height: 100dvh; }
.sidebar {
  width: 360px; max-width: 100%; background: var(--bg); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: relative;
}
.sidebar-header { display: flex; align-items: center; gap: 8px; padding: 8px 12px; position: relative; }
#search {
  flex: 1; border: 0; background: var(--bg-2); border-radius: 22px; padding: 10px 16px; outline: none;
}
#search:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.dropdown {
  position: absolute; top: 52px; left: 12px; z-index: 20; background: var(--bg); border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,.2); padding: 6px; min-width: 240px; display: flex; flex-direction: column;
}
.dropdown button { border: 0; background: transparent; text-align: left; padding: 10px 12px; border-radius: 8px; }
.dropdown button:hover { background: var(--hover); }
.dropdown .me { padding: 10px 12px; color: var(--hint); font-size: 13px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }

.list { overflow-y: auto; flex: 1; padding: 0 8px 8px; }
.list-item {
  display: flex; gap: 12px; align-items: center; padding: 8px 10px; border-radius: 12px; cursor: pointer;
}
.list-item:hover { background: var(--hover); }
.list-item.active { background: var(--active); color: #fff; }
.list-item.active .preview, .list-item.active .time { color: rgba(255,255,255,.85); }
.list-item .info { flex: 1; min-width: 0; }
.list-item .row { display: flex; justify-content: space-between; gap: 8px; }
.list-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item .preview { color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.list-item .time { color: var(--hint); font-size: 12px; flex: none; }
.section-title { color: var(--hint); font-size: 13px; padding: 8px 10px 4px; }

.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: none; display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 18px;
}
.chat-header .avatar { width: 40px; height: 40px; font-size: 16px; }
.badge-bot { font-size: 11px; background: var(--bg-2); color: var(--hint); border-radius: 4px; padding: 0 4px; margin-left: 4px; font-weight: 500; }

/* ---------- chat ---------- */
.chat {
  flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative;
  background: var(--chat-bg);
  background-image: radial-gradient(rgba(255,255,255,.12) 1px, transparent 1px);
  background-size: 18px 18px;
}
.empty-state {
  margin: auto; background: rgba(0,0,0,.25); color: #fff; padding: 6px 14px; border-radius: 16px; font-size: 14px;
}
.chat-header {
  display: flex; align-items: center; gap: 12px; padding: 6px 12px; background: var(--bg);
  border-bottom: 1px solid var(--border); min-height: 56px;
}
.chat-header .back { display: none; }
.chat-title { flex: 1; min-width: 0; }
.chat-title > div:first-child { font-weight: 600; }
.subtitle { color: var(--hint); font-size: 13px; }
.subtitle.typing { color: var(--accent); }

.messages { flex: 1; overflow-y: auto; padding: 12px max(12px, calc((100% - 760px) / 2)); display: flex; flex-direction: column; gap: 4px; }
.day-sep { align-self: center; background: rgba(0,0,0,.25); color: #fff; font-size: 13px; padding: 2px 10px; border-radius: 12px; margin: 8px 0; }
.msg { display: flex; flex-direction: column; max-width: min(520px, 85%); align-self: flex-start; position: relative; }
.msg.out { align-self: flex-end; }
.bubble {
  background: var(--bubble-in); border-radius: var(--radius); padding: 6px 10px 6px; box-shadow: var(--shadow);
  white-space: pre-wrap; overflow-wrap: anywhere; position: relative;
}
.msg.out .bubble { background: var(--bubble-out); }
.bubble .sender { font-weight: 600; font-size: 14px; color: var(--accent); margin-bottom: 2px; }
.bubble .meta { float: right; font-size: 12px; color: var(--hint); margin: 6px 0 -4px 10px; user-select: none; }
.msg.out .bubble .meta { color: var(--bubble-out-meta); }
.bubble .via { color: var(--hint); font-size: 13px; }
.bubble .reply-to {
  border-left: 3px solid var(--accent); padding: 2px 8px; margin-bottom: 4px; border-radius: 4px;
  background: rgba(51,144,236,.08); font-size: 14px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bubble .reply-to b { display: block; color: var(--accent); }
.bubble .webapp-data { color: var(--hint); font-style: italic; }
.bubble code, .bubble pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; background: rgba(0,0,0,.06); border-radius: 4px; }
.bubble code { padding: 0 3px; }
.bubble pre { padding: 6px 8px; margin: 4px 0; overflow-x: auto; white-space: pre; }
.bubble .spoiler { background: var(--hint); color: transparent; border-radius: 3px; cursor: pointer; }
.bubble .spoiler.revealed { background: transparent; color: inherit; }
.bubble blockquote { margin: 4px 0; padding: 2px 8px; border-left: 3px solid var(--accent); }
.bubble .entity-cmd, .bubble .entity-mention { color: var(--link); cursor: pointer; }
.msg-actions {
  position: absolute; top: -12px; right: 4px; display: none; background: var(--bg); border-radius: 14px;
  box-shadow: var(--shadow); padding: 2px;
}
.msg:hover .msg-actions { display: flex; }
.msg-actions button { border: 0; background: transparent; width: 28px; height: 24px; border-radius: 10px; font-size: 13px; }
.msg-actions button:hover { background: var(--hover); }
.msg.highlight .bubble { animation: hl 1.2s; }
@keyframes hl { from { box-shadow: 0 0 0 3px var(--accent); } }

.inline-kb { display: flex; flex-direction: column; gap: 3px; margin-top: 3px; }
.inline-kb .row { display: flex; gap: 3px; }
.inline-kb button {
  flex: 1; border: 0; border-radius: 8px; padding: 8px; background: rgba(0,0,0,.22); color: #fff;
  font-size: 14px; font-weight: 500; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.inline-kb button:hover { background: rgba(0,0,0,.32); }
.inline-kb button.loading { opacity: .6; }
.inline-kb button .kind { font-size: 11px; margin-left: 4px; opacity: .8; }

.bot-intro {
  margin: auto; max-width: 340px; background: rgba(0,0,0,.3); color: #fff; padding: 16px; border-radius: 16px;
  text-align: center; white-space: pre-wrap;
}

.composer-wrap { padding: 8px max(12px, calc((100% - 760px) / 2)) 12px; position: relative; }
.composer {
  display: flex; align-items: flex-end; gap: 6px; background: var(--bg); border-radius: 16px;
  padding: 6px; box-shadow: var(--shadow);
}
.composer textarea {
  flex: 1; border: 0; outline: none; resize: none; background: transparent; padding: 9px 6px; max-height: 160px;
}
.send-btn { border: 0; background: var(--accent); color: #fff; width: 40px; height: 40px; border-radius: 50%; font-size: 18px; flex: none; }
.menu-button {
  border: 0; background: var(--accent); color: #fff; border-radius: 12px; padding: 0 12px; height: 36px;
  font-weight: 600; font-size: 14px; flex: none; max-width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.start-btn {
  width: 100%; border: 0; background: var(--bg); color: var(--accent); font-weight: 700; padding: 14px;
  border-radius: 14px; box-shadow: var(--shadow); letter-spacing: .5px;
}
.reply-preview {
  display: flex; align-items: center; gap: 8px; background: var(--bg); border-radius: 12px 12px 0 0;
  padding: 6px 6px 6px 12px; border-left: 3px solid var(--accent); margin-bottom: -4px; font-size: 14px;
}
.reply-preview > div { flex: 1; min-width: 0; }
.reply-preview #reply-preview-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--hint); }
.reply-preview b { color: var(--accent); }
.commands-popup {
  position: absolute; bottom: calc(100% - 4px); left: max(12px, calc((100% - 760px) / 2)); right: max(12px, calc((100% - 760px) / 2));
  background: var(--bg); border-radius: 12px; box-shadow: 0 4px 24px rgba(0,0,0,.2); max-height: 280px; overflow-y: auto; padding: 4px;
}
.commands-popup button { display: flex; width: 100%; gap: 10px; border: 0; background: transparent; padding: 8px 10px; border-radius: 8px; text-align: left; }
.commands-popup button:hover, .commands-popup button.sel { background: var(--hover); }
.commands-popup .cmd { font-weight: 600; }
.commands-popup .desc { color: var(--hint); }
.reply-keyboard { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.reply-keyboard .row { display: flex; gap: 6px; }
.reply-keyboard button {
  flex: 1; border: 0; background: var(--bg); border-radius: 10px; padding: 12px 8px; box-shadow: var(--shadow); font-weight: 500;
}
.reply-keyboard button:hover { background: var(--hover); }

/* ---------- mini app ---------- */
.webapp-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 50;
}
.webapp-panel {
  width: 420px; max-width: 100%; height: min(760px, 100%); background: var(--bg); border-radius: 14px; overflow: hidden;
  display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0,0,0,.35);
}
.webapp-header { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-bottom: 1px solid var(--border); }
.webapp-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#webapp-frame { flex: 1; border: 0; width: 100%; background: var(--bg); }
.webapp-buttons { display: flex; gap: 8px; padding: 0 8px; }
.webapp-buttons:has(.webapp-btn:not(.hidden)) { padding: 8px; }
.webapp-btn {
  flex: 1; border: 0; border-radius: 10px; padding: 14px; font-weight: 600; background: var(--accent); color: #fff;
}
.webapp-btn:disabled { opacity: .55; cursor: default; }
.webapp-btn.secondary { background: var(--bg-2); color: var(--accent); }
.webapp-btn.progress::after { content: " ⏳"; }

/* ---------- dialog / toast ---------- */
.dialog-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 60; padding: 16px; }
.dialog { background: var(--bg); border-radius: 14px; padding: 20px 20px 10px; width: 320px; max-width: 100%; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.dialog-title { font-weight: 700; margin-bottom: 8px; }
.dialog-title:empty { display: none; }
.dialog-message { white-space: pre-wrap; margin-bottom: 12px; }
.dialog-message input { width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); margin-top: 8px; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 4px; flex-wrap: wrap; }
.dialog-buttons button { border: 0; background: transparent; color: var(--accent); font-weight: 600; padding: 8px 12px; border-radius: 8px; text-transform: uppercase; font-size: 14px; }
.dialog-buttons button:hover { background: var(--hover); }
.dialog-buttons button.destructive { color: var(--danger); }
.toast {
  position: fixed; top: 16px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,.8); color: #fff;
  padding: 10px 16px; border-radius: 10px; z-index: 70; max-width: 90%; font-size: 14px;
}

/* ---------- mobile ---------- */
@media (max-width: 760px) {
  .sidebar { width: 100%; border: 0; }
  .chat { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat { display: flex; }
  .chat-header .back { display: block; }
  .webapp-panel { height: 100%; border-radius: 0; }
}

/* dark-mode contrast tweaks */
:root[data-theme="dark"] .inline-kb button { background: rgba(255,255,255,.1); }
:root[data-theme="dark"] .inline-kb button:hover { background: rgba(255,255,255,.16); }
:root[data-theme="dark"] .msg.out .bubble a,
:root[data-theme="dark"] .msg.out .bubble .entity-cmd { color: #fff; text-decoration: underline; }
:root[data-theme="dark"] .bubble code, :root[data-theme="dark"] .bubble pre { background: rgba(255,255,255,.08); }

/* ---------- branding ---------- */
.tagline { color: var(--hint); margin: -8px 0 16px; font-size: 14px; }
.logo img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.invisible { visibility: hidden; }

/* ---------- in-chat widgets ---------- */
.msg.has-widget { width: min(400px, 92%); }
.widget { white-space: normal; margin-top: 6px; display: flex; flex-direction: column; gap: 8px; }
.w-title { font-weight: 700; font-size: 15px; }
.w-desc { color: var(--hint); font-size: 14px; margin-top: 2px; }
.w-group-title { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; color: var(--hint); margin: 4px 0 6px; }
.w-chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 6px; }
.w-options { display: flex; flex-direction: column; gap: 6px; }
.w-chip {
  border: 1.5px solid var(--border); background: var(--bg); border-radius: 10px; padding: 8px 6px; font-size: 14px; font-weight: 500;
}
.w-options .w-chip { text-align: left; padding: 10px 12px; }
.w-chip:hover:not(:disabled) { border-color: var(--accent); }
.w-chip.selected { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.w-chip.taken, .w-chip:disabled { opacity: .45; cursor: not-allowed; }
.w-chip.taken { text-decoration: line-through; }
.w-submit {
  border: 0; border-radius: 10px; padding: 10px; font-weight: 600; background: var(--accent); color: var(--accent-text);
}
.w-submit:disabled { opacity: .45; cursor: default; }
.w-submit.loading { opacity: .7; }
.w-error { color: var(--danger); font-size: 13px; }
.w-error:empty { display: none; }
.w-footer { color: var(--hint); font-size: 12px; }
.w-done {
  display: flex; gap: 10px; align-items: flex-start; background: var(--bg-2); border-radius: 10px; padding: 10px 12px;
  white-space: pre-wrap; font-size: 14px;
}
.w-check {
  flex: none; width: 22px; height: 22px; border-radius: 50%; background: #4caf50; color: #fff; display: grid; place-items: center;
  font-size: 13px; font-weight: 700;
}
.w-stars { display: flex; gap: 2px; }
.w-stars button { border: 0; background: transparent; font-size: 30px; line-height: 1; color: var(--border); padding: 0 2px; }
.w-stars button.on, .w-stars button.hover { color: #ffb400; }
.w-scale { display: flex; justify-content: space-between; color: var(--hint); font-size: 12px; }
.w-form { display: flex; flex-direction: column; gap: 10px; }
.w-field { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.w-field-checkbox { flex-direction: row; align-items: center; gap: 8px; flex-wrap: wrap; }
.w-label { font-weight: 500; }
.w-label i { color: var(--danger); font-style: normal; }
.w-field input:not([type=checkbox]):not([type=radio]), .w-field select, .w-field textarea {
  border: 1.5px solid var(--border); border-radius: 8px; padding: 8px 10px; background: var(--bg); outline: none; width: 100%;
}
.w-field textarea { min-height: 70px; resize: vertical; }
.w-field input:focus, .w-field select:focus, .w-field textarea:focus { border-color: var(--accent); }
.w-radios { display: flex; flex-direction: column; gap: 4px; }
.w-field-error { color: var(--danger); font-size: 12px; }
.w-field-error:empty { display: none; }
.w-field-checkbox .w-field-error { flex-basis: 100%; }

/* ---------- SSO / menu ---------- */
.sso-buttons { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.sso-buttons:empty { display: none; }
.sso-btn {
  display: block; text-decoration: none; color: var(--text); border: 1.5px solid var(--border); border-radius: 10px;
  padding: 11px; font-weight: 600; background: var(--bg);
}
.sso-btn:hover { border-color: var(--accent); }
.sso-sep { display: flex; align-items: center; gap: 10px; color: var(--hint); font-size: 13px; margin: 6px 0 12px; }
.sso-sep::before, .sso-sep::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.link-btn { border: 0; background: none; color: var(--hint); font-size: 13px; margin-top: 12px; text-decoration: underline; }
.dropdown a { text-decoration: none; color: inherit; padding: 10px 12px; border-radius: 8px; }
.dropdown a:hover { background: var(--hover); }
.w-empty { color: var(--hint); font-size: 14px; padding: 6px 0; }
.dialog-buttons { max-height: 50vh; overflow-y: auto; }

/* ---------- polls ---------- */
.w-kind { font-size: 12px; color: var(--hint); text-transform: uppercase; letter-spacing: .4px; font-weight: 600; }
.w-poll-options { display: flex; flex-direction: column; gap: 6px; }
.w-poll-opt {
  position: relative; overflow: hidden; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  text-align: left; border: 1.5px solid var(--border); background: var(--bg); border-radius: 10px; padding: 9px 12px; font-size: 14px;
}
.w-poll-opt:not(:disabled):hover { border-color: var(--accent); }
.w-poll-opt:disabled { cursor: default; color: inherit; }
.w-poll-opt > *:not(.w-poll-bar) { position: relative; }
.w-poll-bar { position: absolute; inset: 0 auto 0 0; background: var(--accent); opacity: .14; border-radius: 8px 0 0 8px; transition: width .4s ease; }
.w-poll-opt.chosen .w-poll-bar { opacity: .26; }
.w-poll-mark {
  flex: none; width: 20px; height: 20px; border-radius: 50%; border: 1.5px solid var(--border); display: grid; place-items: center;
  font-size: 12px; font-weight: 700; color: var(--accent-text);
}
.w-poll-mark.square { border-radius: 5px; }
.w-poll-opt.chosen .w-poll-mark, .w-poll-opt.picked .w-poll-mark { background: var(--accent); border-color: var(--accent); }
.w-poll-opt.correct { border-color: #4caf50; }
.w-poll-opt.correct .w-poll-bar { background: #4caf50; }
.w-poll-opt.correct .w-poll-mark { background: #4caf50; border-color: #4caf50; }
.w-poll-opt.wrong { border-color: var(--danger); }
.w-poll-opt.wrong .w-poll-bar { background: var(--danger); }
.w-poll-opt.wrong .w-poll-mark { background: var(--danger); border-color: var(--danger); }
.w-poll-label { flex: 1; min-width: 0; }
.w-poll-pct { font-weight: 700; font-size: 13px; }
.w-poll-voters { flex-basis: 100%; padding-left: 30px; font-size: 12px; color: var(--hint); }
.w-poll-footer { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.w-link { border: 0; background: none; color: var(--accent); font-size: 12px; font-weight: 600; padding: 0; }
.w-explanation { background: var(--bg-2); border-radius: 10px; padding: 8px 10px; font-size: 13px; }

/* ---------- advanced forms ---------- */
.w-progress { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--hint); font-weight: 600; }
.w-bar { height: 4px; background: var(--bg-2); border-radius: 2px; overflow: hidden; }
.w-bar i { display: block; height: 100%; background: var(--accent); transition: width .25s; }
.w-page-title { font-weight: 700; margin-top: 4px; }
.w-help { color: var(--hint); font-size: 12px; }
.w-check-row { display: flex; align-items: center; gap: 8px; }
.w-scale-row { display: flex; gap: 4px; flex-wrap: wrap; }
.w-scale-row button {
  flex: 1; min-width: 28px; border: 1.5px solid var(--border); background: var(--bg); border-radius: 8px; padding: 7px 0; font-weight: 600; font-size: 13px;
}
.w-scale-row button.selected { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.w-nav { display: flex; gap: 8px; }
.w-nav .w-submit { flex: 1; }
.w-back { border: 1.5px solid var(--border); background: var(--bg); border-radius: 10px; padding: 10px 14px; font-weight: 600; }
