:root {
  --bg: #f4f5f8;
  --panel: #fff;
  --line: #e5e7ec;
  --text: #1c2330;
  --muted: #747c8c;
  --accent: #1972f5;
  --accent-soft: #e8f0fe;
  --danger: #e5484d;
  --rail: #151a23;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px;
}
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

/* Login */
.login { height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-box { background: var(--panel); padding: 36px 32px; border-radius: 16px; width: 100%; max-width: 360px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.08); }
.login-box h1 { margin: 12px 0 4px; font-size: 22px; }
.login-box p { margin: 0 0 22px; color: var(--muted); }
.login-box input { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 9px; margin-bottom: 12px; outline: none; }
.login-box input:focus { border-color: var(--accent); }
.login-box button { width: 100%; padding: 11px; border: 0; border-radius: 9px; background: var(--accent); color: #fff; font-weight: 600; }
.logo-big { width: 52px; height: 52px; margin: 0 auto; border-radius: 14px; background: var(--accent); color: #fff; font-weight: 800; font-size: 24px; display: flex; align-items: center; justify-content: center; }
.err { color: var(--danger); margin-top: 10px; min-height: 18px; }
.login-box .login-2fa { margin: 0 0 10px; font-size: 13px; }
.site-form + .site-form { margin-top: 14px; }
.site-form h4 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.site-form .status { font-size: 12px; font-weight: 600; }
.site-form .status.ok { color: #1a9b5a; }
.site-form .status.none { color: var(--muted); }
.stack { display: flex; flex-direction: column; gap: 10px; }
.stack input, #tfa-confirm input, #admin-add input { border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; outline: none; font-weight: 400; }
.stack input:focus, #tfa-confirm input:focus { border-color: var(--accent); }
#admin-add .row input { flex: 1; min-width: 160px; }
#tfa-qr { display: block; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.codes { background: #0f141c; color: #d6deeb; padding: 14px 18px; border-radius: 10px; font-size: 14px; line-height: 1.7; margin: 0; column-count: 2; }
.sess-list { display: flex; flex-direction: column; }
.sess { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid #f0f1f4; }
.sess .body { flex: 1; min-width: 0; }
.sess small { color: var(--muted); display: block; }
.badge-you { font-size: 11px; font-weight: 700; color: #1a9b5a; background: #e7f7ee; border-radius: 8px; padding: 1px 7px; margin-left: 6px; }

/* Shell */
.app { display: flex; height: 100%; }
.rail { width: 64px; background: var(--rail); display: flex; flex-direction: column; align-items: center; padding: 14px 0; gap: 8px; flex-shrink: 0; }
.rail .logo { width: 38px; height: 38px; border-radius: 10px; background: var(--accent); color: #fff; font-weight: 800; display: flex; align-items: center; justify-content: center; margin-bottom: 14px; }
.rail-btn { width: 44px; height: 44px; border-radius: 10px; border: 0; background: none; color: #8d96a8; display: flex; align-items: center; justify-content: center; position: relative; }
.rail-btn:hover { background: #222937; color: #fff; }
.rail-btn.active { background: #262e3d; color: #fff; }
.rail-badge { position: absolute; top: 4px; right: 4px; background: var(--danger); color: #fff; font-size: 10px; font-weight: 700; min-width: 17px; height: 17px; border-radius: 9px; display: flex; align-items: center; justify-content: center; padding: 0 4px; }
.rail-spacer { flex: 1; }
.rail-badge.online { background: #1a9b5a; }

/* Online visitors */
.visitors-head h2 { margin: 0; }
.visitors-head select { border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; background: #fff; }
.visitor-list { display: flex; flex-direction: column; gap: 8px; padding-bottom: 30px; }
.vrow { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; display: flex; align-items: center; gap: 12px; }
.vrow .body { flex: 1; min-width: 0; }
.vrow .top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.vrow .page { color: var(--accent); font-size: 13px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-decoration: none; }
.vrow .meta-line { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.pill { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 10px; background: #eef1f6; color: var(--muted); }
.pill.site { background: var(--accent-soft); color: var(--accent); }
.pill.chat { background: #e7f7ee; color: #1a9b5a; }
.pill.new { background: var(--danger); color: #fff; }
.view { flex: 1; display: flex; min-width: 0; }

/* Conversation / site list column */
.convs { width: 320px; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; flex-shrink: 0; }
.convs-head { padding: 18px 16px 10px; display: flex; align-items: center; gap: 10px; }
.convs-head h2 { margin: 0; font-size: 19px; flex: 1; }
.convs-head select { border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; max-width: 160px; background: #fff; }
.tabs { display: flex; gap: 4px; padding: 0 12px 10px; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: none; padding: 7px 12px; border-radius: 7px; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--accent-soft); color: var(--accent); }
.tab-count { background: var(--danger); color: #fff; border-radius: 9px; font-size: 11px; padding: 1px 6px; margin-left: 3px; }
.conv-list { flex: 1; overflow-y: auto; }
.conv { display: flex; gap: 11px; padding: 13px 16px; border-bottom: 1px solid #f0f1f4; cursor: pointer; align-items: flex-start; }
.conv:hover { background: #f8f9fb; }
.conv.active { background: var(--accent-soft); }
.conv .body { flex: 1; min-width: 0; }
.conv .top { display: flex; align-items: baseline; gap: 6px; }
.conv .top b { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .top time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.conv .site { font-size: 11.5px; color: var(--accent); font-weight: 600; }
.conv .preview { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.conv.unread .preview { color: var(--text); font-weight: 600; }
.conv .count { background: var(--accent); color: #fff; border-radius: 10px; font-size: 11px; font-weight: 700; padding: 1px 7px; align-self: center; }
.avatar { width: 38px; height: 38px; border-radius: 50%; color: #fff; font-weight: 700; display: flex; align-items: center; justify-content: center; flex-shrink: 0; position: relative; text-transform: uppercase; }
.avatar.online::after { content: ""; position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%; background: #2ed47a; border: 2px solid #fff; }
.list-empty { padding: 40px 20px; text-align: center; color: var(--muted); }

/* Thread */
.thread { flex: 1; display: flex; min-width: 0; background: var(--bg); }
.thread-inner { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.thread-head { display: flex; align-items: center; gap: 12px; padding: 12px 18px; background: var(--panel); border-bottom: 1px solid var(--line); }
.thread-who { flex: 1; min-width: 0; }
.thread-who b { display: block; font-size: 15px; }
.thread-who span { color: var(--muted); font-size: 12.5px; }
.msgs { flex: 1; overflow-y: auto; padding: 20px 24px; display: flex; flex-direction: column; gap: 4px; }
.msg { max-width: min(560px, 75%); padding: 10px 14px; border-radius: 16px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg a { color: inherit; }
.msg.visitor { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 5px; }
.msg.operator { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 5px; }
.meta { font-size: 11px; color: var(--muted); margin: 2px 6px 10px; }
.meta.operator { align-self: flex-end; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: #e9ebf0; padding: 3px 10px; border-radius: 10px; margin: 10px 0; }
.typing-row { color: var(--muted); font-size: 12.5px; font-style: italic; padding: 4px 6px; }
.composer { display: flex; gap: 10px; padding: 12px 18px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { flex: 1; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; resize: none; outline: none; max-height: 160px; line-height: 1.4; }
.composer textarea:focus { border-color: var(--accent); }

.btn { border: 1px solid var(--line); background: #fff; padding: 8px 14px; border-radius: 8px; font-weight: 600; white-space: nowrap; }
.btn:hover { background: #f6f7f9; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.icon-btn { border: 0; background: none; width: 34px; height: 34px; border-radius: 8px; color: var(--muted); display: flex; align-items: center; justify-content: center; }
.icon-btn:hover { background: #f1f2f5; color: var(--text); }
.mobile-only { display: none; }

.empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); text-align: center; padding: 20px; }
.empty-ic { font-size: 42px; margin-bottom: 8px; }
.empty p { margin: 0 0 4px; font-size: 16px; color: var(--text); font-weight: 600; }

/* Visitor sidebar */
.visitor { width: 270px; background: var(--panel); border-left: 1px solid var(--line); padding: 18px; overflow-y: auto; flex-shrink: 0; }
.visitor h3 { margin: 0 0 14px; font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.visitor dl { margin: 0; }
.visitor dt { font-size: 12px; color: var(--muted); margin-top: 12px; }
.visitor dd { margin: 2px 0 0; overflow-wrap: anywhere; }
.visitor dd a { color: var(--accent); }

/* Websites */
.add-site { display: flex; gap: 8px; padding: 0 16px 14px; border-bottom: 1px solid var(--line); }
.add-site input { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; outline: none; }
.add-site input:focus { border-color: var(--accent); }
.swatch { width: 38px; height: 38px; border-radius: 10px; flex-shrink: 0; color: #fff; font-weight: 700; display: flex; align-items: center; justify-content: center; }
.site-detail { flex: 1; overflow-y: auto; padding: 28px 32px; display: flex; flex-direction: column; }
.site-detail > div:not(.empty) { max-width: 760px; width: 100%; }
.site-detail h2 { margin: 0 0 18px; }
.site-detail h3 { margin: 34px 0 6px; }
.site-form { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.site-form label { display: flex; flex-direction: column; gap: 5px; font-weight: 600; font-size: 13px; }
.site-form label small { font-weight: 400; color: var(--muted); }
.site-form input:not([type=radio]):not([type=checkbox]):not([type=color]), .site-form textarea { border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; outline: none; font-weight: 400; }
.site-form input:focus, .site-form textarea:focus { border-color: var(--accent); }
.site-form input[type=color] { width: 60px; height: 38px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: #fff; }
.site-form fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.site-form legend { font-weight: 600; font-size: 13px; padding: 0 4px; }
.site-form label.radio { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; font-size: 14px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; }
.saved { color: #1a9b5a; font-weight: 600; }
.hint { color: var(--muted); margin: 4px 0 10px; }
.hint a { color: var(--accent); font-weight: 600; }
.code-box { position: relative; background: #0f141c; border-radius: 10px; }
.code-box pre { margin: 0; padding: 16px 18px; padding-right: 86px; color: #d6deeb; font-size: 12.5px; line-height: 1.55; white-space: pre-wrap; word-break: break-all; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.code-box .btn { position: absolute; top: 10px; right: 10px; }

/* Identity verification */
.muted { color: var(--muted); font-weight: 400; font-size: 13px; }
.verified { color: #1a9b5a; font-weight: 600; font-size: 12px; white-space: nowrap; }
.conv .top .verified { flex-shrink: 0; }
.secret-row { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.secret-row input { flex: 1; min-width: 200px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px; background: #fff; }
.lang-tabs { display: flex; gap: 4px; margin-bottom: 8px; }

/* AI */
.msg.ai { background: #f1ecff; color: var(--text); border: 1px solid #ddd2ff; }
.meta.ai { align-self: flex-end; }
.ai-chip.on { color: #6b3fe0; border-color: #d9ccff; background: #f6f2ff; }
.ai-chip.paused { color: var(--muted); }
.banner { padding: 9px 18px; font-size: 13px; background: #fff7e6; color: #8a5a00; border-bottom: 1px solid #f5e1b5; }
.banner.warn { border: 1px solid #f5e1b5; border-radius: 10px; margin-bottom: 14px; }
.banner a { color: inherit; font-weight: 600; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1c2330; color: #fff; padding: 11px 18px; border-radius: 10px; font-size: 13.5px; box-shadow: 0 8px 30px rgba(0,0,0,.25); z-index: 10; max-width: calc(100% - 32px); cursor: pointer; }
.site-form select { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; font-weight: 400; }
#ag-kbs { display: flex; flex-direction: column; gap: 6px; }
.provider-keys { display: flex; flex-direction: column; gap: 14px; }
.provider-keys h4 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.provider-keys .status { font-size: 12px; font-weight: 600; }
.provider-keys .status.ok { color: #1a9b5a; }
.provider-keys .status.none { color: var(--muted); }
.provider-keys .row input { flex: 1; min-width: 180px; border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; outline: none; }
.playground { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; height: 420px; }
.playground .msgs { background: var(--bg); }
.sources { font-size: 11.5px; color: var(--muted); margin: -6px 6px 10px; }
.row.wrap { flex-wrap: wrap; margin-bottom: 14px; }
.grow-input { flex: 1; min-width: 200px; }
.file-btn { cursor: pointer; display: inline-flex; align-items: center; font-weight: 600 !important; font-size: 14px !important; }
#article-form { display: flex; flex-direction: column; gap: 14px; }
.kb-title { margin-bottom: 4px; }
.title-input { flex: 1; font-size: 22px; font-weight: 700; border: 1px solid transparent; border-radius: 8px; padding: 4px 8px; margin-left: -8px; background: transparent; outline: none; min-width: 0; }
.title-input:hover, .title-input:focus { border-color: var(--line); background: #fff; }
.article-list { display: flex; flex-direction: column; gap: 8px; padding-bottom: 30px; }
.article { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; display: flex; gap: 12px; align-items: flex-start; }
.article .body { flex: 1; min-width: 0; }
.article b { display: block; }
.article p { margin: 3px 0 0; color: var(--muted); font-size: 13px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.article a { font-size: 12px; color: var(--accent); }

/* Responsive */
@media (max-width: 1100px) { .visitor { display: none !important; } }
@media (max-width: 760px) {
  .app { flex-direction: column-reverse; }
  .rail { width: 100%; flex-direction: row; height: 56px; padding: 0 12px; justify-content: space-around; }
  .rail .logo, .rail-spacer { display: none; }
  .view { min-height: 0; }
  .convs { width: 100%; border-right: 0; }
  .inbox.has-thread .convs, .sites.has-site .convs { display: none; }
  .inbox:not(.has-thread) .thread, .sites:not(.has-site) .site-detail { display: none; }
  .split.has-detail .convs { display: none; }
  .split:not(.has-detail) .site-detail { display: none; }
  .mobile-only { display: flex; }
  .site-detail { padding: 18px 16px; }
  .grid { grid-template-columns: 1fr; }
  .msgs { padding: 16px; }
}
