/* ================= tokens ================= */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e4e7ec;
  --border-strong: #d0d5dd;
  --text: #101828;
  --text-2: #475467;
  --muted: #667085;
  --accent: #444ce7;
  --accent-hover: #3538cd;
  --accent-soft: #eef0ff;
  --danger: #d92d20;
  --danger-soft: #fef3f2;
  --ok: #067647;
  --ok-soft: #ecfdf3;
  --warn: #b54708;
  --warn-soft: #fffaeb;
  --sidebar: #101828;
  --radius: 8px;
  --shadow-sm: 0 1px 2px rgb(16 24 40 / .05);
  --shadow-md: 0 4px 8px -2px rgb(16 24 40 / .1), 0 2px 4px -2px rgb(16 24 40 / .06);
}
* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; }
body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
svg { width: 18px; height: 18px; flex-shrink: 0; }
a { color: inherit; text-decoration: none; }

/* ================= login ================= */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); padding: 20px; }
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 40px; width: 380px; box-shadow: var(--shadow-md); text-align: center;
}
.login-mark {
  width: 48px; height: 48px; border-radius: 10px; background: var(--accent); color: #fff;
  font-weight: 700; font-size: 18px; display: flex; align-items: center; justify-content: center;
  margin: 0 auto 16px; letter-spacing: .5px;
}
.login-card h1 { font-size: 20px; font-weight: 650; }
.login-sub { color: var(--muted); font-size: 13.5px; margin: 4px 0 28px; }
.field-label { display: block; text-align: left; font-size: 13px; font-weight: 550; color: var(--text-2); margin: 0 0 6px; }
.login-card input {
  width: 100%; padding: 10px 13px; border: 1px solid var(--border-strong); border-radius: var(--radius);
  font-size: 14px; margin-bottom: 16px; outline: none; transition: border-color .15s, box-shadow .15s;
  font-family: inherit;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.login-error { color: var(--danger); font-size: 13px; margin-top: 14px; }

/* ================= shell ================= */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex-shrink: 0; background: var(--sidebar); color: #98a2b3;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { color: #fff; font-weight: 650; font-size: 15px; padding: 20px 20px 24px; display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: #fff;
  font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center;
}
.sidebar nav { flex: 1; padding: 0 12px; display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a {
  display: flex; align-items: center; gap: 11px; padding: 9px 12px; border-radius: 7px;
  font-size: 13.5px; font-weight: 500; transition: background .12s, color .12s;
}
.sidebar nav a:hover { background: rgb(255 255 255 / .06); color: #e4e7ec; }
.sidebar nav a.active { background: rgb(255 255 255 / .1); color: #fff; }
.sidebar-foot {
  padding: 14px 16px; border-top: 1px solid rgb(255 255 255 / .08);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.me { color: #e4e7ec; font-size: 13px; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  background: transparent; border: none; color: #98a2b3; cursor: pointer; padding: 7px;
  border-radius: 7px; display: flex; transition: background .12s, color .12s;
}
.icon-btn:hover { background: rgb(255 255 255 / .08); color: #fff; }

.main { flex: 1; min-width: 0; padding: 28px 36px 64px; max-width: 1160px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h1 { font-size: 19px; font-weight: 650; letter-spacing: -.01em; }
.page-head .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.back-link {
  display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px;
  font-weight: 500; margin-bottom: 10px; cursor: pointer;
}
.back-link:hover { color: var(--text); }
.back-link svg { width: 15px; height: 15px; }

/* ================= components ================= */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow-sm); margin-bottom: 16px;
}
.card-pad { padding: 18px 20px; }
.card-title { font-weight: 600; font-size: 14px; margin-bottom: 4px; }
.card-sub { color: var(--muted); font-size: 12.5px; margin-bottom: 14px; }
.section-label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 0 8px; }
.section-label:first-child { margin-top: 0; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

.btn {
  display: inline-flex; align-items: center; gap: 7px; border: 1px solid transparent;
  border-radius: var(--radius); padding: 8px 14px; font-size: 13.5px; font-weight: 550;
  cursor: pointer; transition: background .12s, border-color .12s; font-family: inherit;
  white-space: nowrap;
}
.btn svg { width: 15px; height: 15px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-outline { background: var(--surface); color: var(--text-2); border-color: var(--border-strong); }
.btn-outline:hover { background: var(--bg); }
.btn-danger { background: var(--surface); color: var(--danger); border-color: var(--border-strong); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-ok { background: var(--ok); color: #fff; }
.btn-ok:hover { filter: brightness(1.08); }
.btn-sm { padding: 5px 11px; font-size: 12.5px; }
.btn-block { width: 100%; justify-content: center; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.input, select.input {
  border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 8px 12px;
  font-size: 13.5px; font-family: inherit; background: var(--surface); color: var(--text);
  outline: none; transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
textarea.input { width: 100%; min-height: 88px; resize: vertical; line-height: 1.55; }

.badge {
  display: inline-flex; align-items: center; gap: 6px; border-radius: 99px; padding: 2px 10px;
  font-size: 12px; font-weight: 550; white-space: nowrap; border: 1px solid transparent;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 99px; background: currentColor; }
.badge.learn { background: #f2f4f7; color: #475467; border-color: #e4e7ec; }
.badge.need_permission { background: var(--warn-soft); color: var(--warn); border-color: #fedf89; }
.badge.full { background: var(--ok-soft); color: var(--ok); border-color: #abefc6; }
.badge.staff { background: var(--ok-soft); color: var(--ok); border-color: #abefc6; }
.badge.customer { background: #f2f4f7; color: #475467; border-color: #e4e7ec; }
.badge.unknown { background: #f2f4f7; color: #98a2b3; border-color: #e4e7ec; }
.badge.plain::before { display: none; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th {
  text-align: left; color: var(--muted); font-weight: 550; font-size: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: #fafbfc;
}
td { padding: 11px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: #fafbfc; }

.empty { color: var(--muted); text-align: center; padding: 40px 16px; font-size: 13.5px; }
code {
  background: #f2f4f7; border: 1px solid var(--border); padding: 2px 7px; border-radius: 6px;
  font-size: 12.5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.count-pill {
  display: inline-flex; min-width: 20px; height: 20px; padding: 0 6px; align-items: center;
  justify-content: center; border-radius: 99px; background: var(--danger); color: #fff;
  font-size: 11.5px; font-weight: 650;
}
.count-pill.zero { background: #f2f4f7; color: var(--muted); }

/* ================= company workspace ================= */
.ws-head { margin-bottom: 18px; }
.ws-title { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.ws-title h1 { font-size: 19px; font-weight: 650; }
.ws-controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 12px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 18px; }
.tabs button {
  background: none; border: none; border-bottom: 2px solid transparent; color: var(--muted);
  font-size: 13.5px; font-weight: 550; padding: 9px 14px; cursor: pointer; font-family: inherit;
  display: inline-flex; align-items: center; gap: 7px;
}
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
.tabs .count-pill { height: 18px; min-width: 18px; font-size: 11px; }

/* chat */
.chat-layout {
  display: grid; grid-template-columns: 240px 1fr; gap: 0; border: 1px solid var(--border);
  border-radius: 10px; background: var(--surface); box-shadow: var(--shadow-sm);
  height: calc(100vh - 300px); min-height: 420px; overflow: hidden;
}
.conv-list { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.conv-list-head { padding: 12px; border-bottom: 1px solid var(--border); }
.conv-items { flex: 1; overflow-y: auto; padding: 6px; }
.conv-item {
  padding: 9px 11px; border-radius: 7px; cursor: pointer; font-size: 13px;
  color: var(--text-2); display: flex; align-items: center; justify-content: space-between; gap: 6px;
}
.conv-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 550; }
.conv-actions { display: none; flex-shrink: 0; }
.conv-item:hover .conv-actions, .conv-item.active .conv-actions { display: inline-flex; }
.conv-item .icon-btn { color: var(--muted); padding: 3px; }
.conv-item .icon-btn:hover { background: none; color: var(--text); }
.conv-item .icon-btn:last-child:hover { color: var(--danger); }
.conv-item .icon-btn svg { width: 13px; height: 13px; }

.thread { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.thread-log { flex: 1; min-height: 0; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 76%; padding: 10px 14px; border-radius: 12px; word-break: break-word; font-size: 13.5px; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: #f2f4f7; border-bottom-left-radius: 4px; max-width: 86%; }
.msg.pending { color: var(--muted); font-style: italic; background: none; padding: 4px 2px; }

/* markdown inside assistant bubbles */
.msg.md p { margin: 0 0 8px; }
.msg.md > *:last-child { margin-bottom: 0; }
.msg.md ul, .msg.md ol { margin: 0 0 8px; padding-left: 20px; }
.msg.md li { margin: 3px 0; }
.msg.md h3, .msg.md h4, .msg.md h5, .msg.md h6 { margin: 12px 0 6px; font-size: 13.5px; font-weight: 650; }
.msg.md > h3:first-child, .msg.md > h4:first-child { margin-top: 0; }
.msg.md pre {
  background: #e8eaf0; border: none; margin: 0 0 8px; padding: 10px 12px; border-radius: 8px;
  font-size: 12.5px; overflow-x: auto; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.msg.md code { background: rgb(16 24 40 / .08); border: none; padding: 1px 5px; border-radius: 5px; font-size: 12.5px; }
.msg.md pre code { background: none; padding: 0; }
.msg.md blockquote { border-left: 3px solid var(--border-strong); padding-left: 10px; color: var(--text-2); margin: 0 0 8px; }
.msg.md hr { border: none; border-top: 1px solid var(--border-strong); margin: 10px 0; }
.msg.md table { margin: 0 0 8px; font-size: 12.5px; background: var(--surface); border-radius: 6px; }
.msg.md th, .msg.md td { padding: 6px 10px; }
.msg.md a { color: var(--accent); text-decoration: underline; }

/* image attachments */
.msg-img { max-width: 220px; max-height: 220px; border-radius: 8px; display: block; margin: 0 0 6px; }
.attach-preview { display: none; gap: 8px; padding: 10px 12px 0; flex-wrap: wrap; border-top: 1px solid var(--border); }
.attach-thumb { position: relative; }
.attach-thumb img { width: 52px; height: 52px; object-fit: cover; border-radius: 7px; border: 1px solid var(--border); }
.attach-x {
  position: absolute; top: -6px; right: -6px; width: 18px; height: 18px; border-radius: 99px;
  background: var(--text); color: #fff; border: none; cursor: pointer; display: flex;
  align-items: center; justify-content: center; padding: 0;
}
.attach-x svg { width: 10px; height: 10px; }
.attach-btn { color: var(--muted); align-self: center; }
.attach-btn:hover { background: var(--bg); color: var(--text); }
.asset-thumb { width: 56px; height: 56px; object-fit: cover; border-radius: 7px; border: 1px solid var(--border); display: block; }

/* telegram group conversation log */
.tg-log { max-height: calc(100vh - 320px); min-height: 300px; overflow-y: auto; padding: 8px 20px 16px; }
.tg-msg { padding: 9px 0; border-bottom: 1px solid var(--border); }
.tg-msg:last-child { border-bottom: none; }
.tg-meta { display: flex; align-items: center; gap: 8px; margin-bottom: 2px; }
.tg-text { color: var(--text-2); white-space: pre-wrap; word-break: break-word; }
.thread-input { display: flex; gap: 10px; padding: 12px; border-top: 1px solid var(--border); align-items: flex-end; }
.thread-input textarea {
  flex: 1; border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 9px 13px;
  font-size: 13.5px; font-family: inherit; resize: none; outline: none; max-height: 130px; line-height: 1.5;
}
.thread-input textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.thread-hint { margin: auto; text-align: center; color: var(--muted); font-size: 13px; max-width: 360px; }

/* memory editor */
.mem-list { display: flex; flex-direction: column; gap: 8px; }
.mem-item { display: flex; gap: 8px; align-items: center; }
.mem-item .input { flex: 1; }
.mem-item .icon-btn { color: var(--muted); }
.mem-item .icon-btn:hover { background: var(--danger-soft); color: var(--danger); }
.docs-table td { padding: 6px 8px; border-bottom: none; }
.docs-table th { padding: 6px 8px; background: none; border-bottom: 1px solid var(--border); }
.docs-table .input { width: 100%; }
.add-row-btn { margin-top: 8px; }

/* checkbox */
.check { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }

/* ================= responsive ================= */
@media (max-width: 820px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; flex-direction: row; align-items: center; }
  .brand { padding: 12px 14px; }
  .sidebar nav { flex-direction: row; overflow-x: auto; padding: 8px 6px; }
  .sidebar nav a span { display: none; }
  .sidebar-foot { border-top: none; margin-left: auto; }
  .me { display: none; }
  .main { padding: 18px 14px 48px; }
  .chat-layout { grid-template-columns: 1fr; height: auto; }
  .conv-list { border-right: none; border-bottom: 1px solid var(--border); max-height: 180px; }
  .thread { height: 60vh; }
}
