:root {
  --bg: #f5f5f7;
  --panel: #ffffff;
  --panel-2: #fafafb;
  --line: #ebebef;
  --line-2: #e2e2e8;
  --text: #18181b;
  --text-2: #6b7280;
  --text-3: #a1a1aa;
  --accent: #4f46e5;
  --accent-2: #4338ca;
  --accent-soft: #eef2ff;
  --green: #16a34a;
  --amber: #d97706;
  --red: #dc2626;
  --radius: 12px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-lg: 0 24px 60px -12px rgba(16, 24, 40, 0.22);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); font-size: 12px; }

/* ---------------------------------- login --------------------------------- */

.login {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 20%, #eef2ff 0%, var(--bg) 55%);
  z-index: 50;
}
.login-card {
  width: 340px;
  padding: 34px 30px 26px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.brand-mark {
  width: 44px; height: 44px; margin: 0 auto 14px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  border-radius: 12px; font-weight: 600; letter-spacing: 0.5px;
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; font-weight: 600; }
.login-card p { margin: 0 0 20px; font-size: 12.5px; }
.login-card input {
  width: 100%; padding: 11px 13px; margin-bottom: 12px;
  border: 1px solid var(--line-2); border-radius: 10px; outline: none;
  background: var(--panel-2);
}
.login-card input:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.hint { margin-top: 14px; font-size: 11.5px; color: var(--text-3); }

/* ----------------------------------- shell -------------------------------- */

.app {
  display: grid;
  grid-template-columns: 64px 322px minmax(0, 1fr) 296px;
  height: 100vh;
  overflow: hidden;
}

.rail {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 14px 0 12px;
  background: #101014;
  color: #8b8b94;
}
.rail-logo {
  width: 34px; height: 34px; margin-bottom: 12px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  border-radius: 9px; font-size: 12px; font-weight: 600;
}
.rail-btn {
  position: relative;
  width: 38px; height: 38px; display: grid; place-items: center;
  border-radius: 10px; color: #9c9ca6; transition: 0.15s;
}
.rail-btn:hover { background: #1e1e26; color: #e6e6ea; }
.rail-btn.active { background: var(--accent); color: #fff; }
.rail-btn svg { width: 19px; height: 19px; }
.rail-badge {
  position: absolute; top: 2px; right: 2px;
  min-width: 16px; height: 16px; padding: 0 4px;
  display: none; place-items: center;
  background: #ef4444; color: #fff;
  border-radius: 8px; font-size: 10px; font-weight: 600;
}
.rail-badge.show { display: grid; }
.rail-sep { width: 26px; height: 1px; background: #26262e; margin: 6px 0; }
.rail-spacer { flex: 1; }
#railChannels { display: flex; flex-direction: column; gap: 4px; }

/* ----------------------------------- list --------------------------------- */

.list {
  display: flex; flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-width: 0;
}
.list-head { padding: 16px 16px 10px; border-bottom: 1px solid var(--line); }
.list-title { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.list-title h2 { margin: 0; font-size: 15px; font-weight: 600; }
.pill {
  padding: 1px 7px; border-radius: 20px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 11.5px; font-weight: 600;
}
.search { position: relative; margin-bottom: 10px; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 15px; height: 15px; color: var(--text-3); }
.search input {
  width: 100%; padding: 8px 10px 8px 32px;
  border: 1px solid var(--line-2); border-radius: 9px;
  background: var(--panel-2); outline: none; font-size: 13px;
}
.search input:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.filters { display: flex; gap: 6px; }
.chip {
  padding: 4px 10px; border-radius: 20px;
  font-size: 12px; color: var(--text-2);
  border: 1px solid transparent; transition: 0.15s;
}
.chip:hover { background: var(--panel-2); }
.chip.active { background: var(--accent-soft); color: var(--accent); font-weight: 500; }

.conv-list { flex: 1; overflow-y: auto; padding: 6px 8px 16px; }
.conv {
  display: grid; grid-template-columns: 40px minmax(0, 1fr) auto;
  gap: 10px; padding: 10px; border-radius: 10px;
  cursor: pointer; transition: background 0.12s;
  border: 1px solid transparent;
}
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--accent-soft); border-color: #dfe3ff; }
.conv.unread .conv-name { font-weight: 600; }
.conv.unread .conv-preview { color: var(--text); }
.avatar {
  width: 40px; height: 40px; border-radius: 11px;
  display: grid; place-items: center;
  color: #fff; font-size: 14px; font-weight: 600;
  position: relative; flex: none;
}
.avatar .dot {
  position: absolute; right: -2px; bottom: -2px;
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid #fff;
}
.conv-main { min-width: 0; }
.conv-name-row { display: flex; align-items: center; gap: 6px; }
.conv-name { font-size: 13.5px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-preview {
  margin-top: 3px; font-size: 12.5px; color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-side { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; }
.conv-time { font-size: 11px; color: var(--text-3); white-space: nowrap; }
.unread-dot {
  min-width: 17px; height: 17px; padding: 0 5px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  border-radius: 9px; font-size: 10.5px; font-weight: 600;
}
.status-line { display: flex; gap: 4px; align-items: center; }

/* ----------------------------------- chat --------------------------------- */

.chat { display: flex; flex-direction: column; background: var(--panel-2); min-width: 0; }

.empty-state {
  margin: auto; text-align: center; max-width: 380px; padding: 24px;
}
.empty-icon {
  width: 54px; height: 54px; margin: 0 auto 16px;
  display: grid; place-items: center;
  background: #fff; border: 1px solid var(--line); border-radius: 14px;
  color: var(--accent);
}
.empty-icon svg { width: 26px; height: 26px; }
.empty-state h3 { margin: 0 0 6px; font-size: 16px; font-weight: 600; }
.empty-state p { margin: 0 0 22px; color: var(--text-2); font-size: 13px; line-height: 1.6; }
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat {
  background: #fff; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 8px;
}
.stat b { display: block; font-size: 18px; font-weight: 600; }
.stat span { font-size: 11px; color: var(--text-2); }

.chat-body { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; background: #fff; border-bottom: 1px solid var(--line);
}
.chat-head .avatar { width: 38px; height: 38px; border-radius: 10px; }
.chat-title { min-width: 0; flex: 1; }
.chat-name-row { display: flex; align-items: center; gap: 8px; }
.chat-title h2 { margin: 0; font-size: 15px; font-weight: 600; }
.channel-tag {
  padding: 2px 8px; border-radius: 6px;
  font-size: 11px; font-weight: 500;
  background: var(--panel-2); border: 1px solid var(--line);
  color: var(--text-2);
}
.chat-actions { display: flex; gap: 8px; align-items: center; }

.messages {
  flex: 1; overflow-y: auto;
  padding: 20px 24px 8px;
  display: flex; flex-direction: column; gap: 12px;
}
.day-sep { text-align: center; font-size: 11px; color: var(--text-3); margin: 6px 0; }
.msg { display: flex; gap: 9px; max-width: 74%; animation: pop 0.18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; flex-direction: row-reverse; }
.msg .avatar { width: 28px; height: 28px; border-radius: 8px; font-size: 11px; }
.bubble {
  padding: 9px 13px; border-radius: 14px;
  font-size: 13.5px; line-height: 1.62;
  white-space: pre-wrap; word-break: break-word;
  background: #fff; border: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
}
.msg.out .bubble {
  background: var(--accent); color: #fff; border-color: transparent;
  border-bottom-right-radius: 4px;
}
.msg.in .bubble { border-bottom-left-radius: 4px; }
.msg-meta {
  margin-top: 5px; font-size: 11px; color: var(--text-3);
  display: flex; gap: 6px; align-items: center;
}
.msg.out .msg-meta { justify-content: flex-end; }
.ai-badge {
  padding: 1px 6px; border-radius: 5px;
  background: #f0fdf4; color: #15803d;
  border: 1px solid #dcfce7; font-size: 10px; font-weight: 500;
}
.fail-badge { color: var(--red); font-size: 10.5px; }

.composer { padding: 10px 18px 14px; background: #fff; border-top: 1px solid var(--line); }
.quick-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.quick {
  padding: 4px 10px; border-radius: 7px; font-size: 12px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text-2);
}
.quick:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.input-row { display: flex; gap: 10px; align-items: flex-end; }
.input-row textarea {
  flex: 1; resize: none; max-height: 140px;
  padding: 10px 12px; border: 1px solid var(--line-2); border-radius: 10px;
  background: var(--panel-2); outline: none; line-height: 1.55;
}
.input-row textarea:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.composer-hint { display: flex; justify-content: space-between; margin-top: 7px; font-size: 11px; color: var(--text-3); }
.kbd { font-family: var(--mono); font-size: 10.5px; padding: 1px 5px; border: 1px solid var(--line); border-radius: 4px; }

/* ----------------------------------- side --------------------------------- */

.side {
  background: var(--panel); border-left: 1px solid var(--line);
  overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 16px;
}
.side-block h4 {
  margin: 0 0 10px; font-size: 11px; font-weight: 600;
  letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-3);
}
.kv { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; font-size: 12.5px; }
.kv span { color: var(--text-2); }
.kv b { font-weight: 500; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.switch-row { display: flex; justify-content: space-between; align-items: center; font-size: 13px; }
.switch {
  appearance: none; width: 36px; height: 20px; border-radius: 20px;
  background: #d4d4d8; position: relative; transition: 0.18s; cursor: pointer; flex: none;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%; background: #fff;
  transition: 0.18s; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(16px); }

.tag-wrap { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 8px; }
.tag {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 8px; border-radius: 6px;
  background: var(--accent-soft); color: var(--accent); font-size: 11.5px;
}
.tag button { color: inherit; opacity: 0.6; font-size: 12px; line-height: 1; }
.tag button:hover { opacity: 1; }
.tag-add input {
  width: 100%; padding: 6px 9px; font-size: 12.5px;
  border: 1px dashed var(--line-2); border-radius: 8px; background: transparent; outline: none;
}
.tag-add input:focus { border-color: var(--accent); border-style: solid; }

.kb-hits { display: flex; flex-direction: column; gap: 6px; }
.kb-hit {
  padding: 8px 10px; border-radius: 8px;
  background: var(--panel-2); border: 1px solid var(--line);
}
.kb-hit b { display: block; font-size: 12px; font-weight: 600; margin-bottom: 3px; }
.kb-hit p { margin: 0; font-size: 11.5px; color: var(--text-2); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.kb-hit .score { float: right; font-size: 10px; color: var(--text-3); }

.snippet {
  margin: 0 0 8px; padding: 10px; border-radius: 8px;
  background: #0f0f14; color: #d4d4d8;
  font-family: var(--mono); font-size: 11px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-all;
  max-height: 140px; overflow: auto;
}
.side-block.grow { margin-top: auto; }

/* ---------------------------------- buttons -------------------------------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 13px; border-radius: 9px;
  font-size: 12.5px; font-weight: 500;
  border: 1px solid var(--line-2); background: #fff; color: var(--text);
  transition: 0.15s;
}
.btn:hover { border-color: #d4d4d8; background: var(--panel-2); }
.btn svg { width: 15px; height: 15px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); border-color: #fecaca; background: #fef2f2; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.select { padding-right: 8px; }
.icon-btn { width: 30px; height: 30px; border-radius: 8px; color: var(--text-2); font-size: 14px; }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

/* ---------------------------------- modals --------------------------------- */

.modal {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(16, 18, 27, 0.42);
  display: grid; place-items: center; padding: 24px;
  backdrop-filter: blur(2px);
}
.modal-card {
  width: 100%; max-width: 560px; max-height: 88vh;
  display: flex; flex-direction: column;
  background: #fff; border-radius: 16px; box-shadow: var(--shadow-lg);
  overflow: hidden; animation: pop 0.16s ease-out;
}
.modal-card.wide { max-width: 780px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--line);
}
.modal-head h3 { margin: 0; font-size: 15px; font-weight: 600; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; border-top: 1px solid var(--line); background: var(--panel-2);
}
.tabs { display: flex; gap: 4px; padding: 10px 16px 0; border-bottom: 1px solid var(--line); }
.tab { padding: 7px 12px; font-size: 13px; color: var(--text-2); border-radius: 8px 8px 0 0; }
.tab.active { color: var(--accent); font-weight: 500; box-shadow: inset 0 -2px 0 var(--accent); }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; margin-bottom: 6px; font-size: 12.5px; color: var(--text-2); }
.field input[type="text"], .field input[type="password"], .field input:not([type]), .field textarea, .field select {
  width: 100%; padding: 9px 11px;
  border: 1px solid var(--line-2); border-radius: 9px;
  background: var(--panel-2); outline: none; font-size: 13px;
}
.field input:focus, .field textarea:focus, .field select:focus {
  border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea { resize: vertical; line-height: 1.6; }
.row-gap { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.channel-card {
  border: 1px solid var(--line); border-radius: 12px; margin-bottom: 12px; overflow: hidden;
}
.channel-card header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; background: var(--panel-2);
}
.channel-card header .ch-dot { width: 9px; height: 9px; border-radius: 50%; }
.channel-card header b { font-size: 13.5px; font-weight: 600; }
.channel-card header .ch-state { margin-left: auto; font-size: 11px; }
.channel-card .ch-body { padding: 14px; border-top: 1px solid var(--line); }
.channel-card .field { margin-bottom: 10px; }
.channel-card .field > span { font-size: 11.5px; }
.channel-card .field input { font-size: 12.5px; padding: 7px 10px; }

.kb-layout { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: 420px; max-height: 70vh; }
.kb-list { border-right: 1px solid var(--line); overflow-y: auto; padding: 10px; }
.kb-item {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; margin-bottom: 4px;
  border: 1px solid transparent;
}
.kb-item:hover { background: var(--panel-2); }
.kb-item.active { background: var(--accent-soft); border-color: #dfe3ff; }
.kb-item b { display: block; font-size: 12.5px; font-weight: 500; }
.kb-item span { font-size: 11px; color: var(--text-3); }
.kb-editor { padding: 18px 20px; overflow-y: auto; }
.kb-test { display: flex; gap: 8px; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--line); }
.kb-test input {
  flex: 1; padding: 8px 11px; border: 1px solid var(--line-2);
  border-radius: 9px; background: var(--panel-2); outline: none; font-size: 12.5px;
}

/* --------------------------------- ticketing -------------------------------- */

.t-badge {
  padding: 1px 7px; border-radius: 6px;
  font-size: 10.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.03em;
  background: var(--panel-2); border: 1px solid var(--line);
}
.t-assignee {
  max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 1px 7px; border-radius: 6px;
  font-size: 10.5px; color: var(--text-2);
  background: var(--accent-soft); border: 1px solid #dfe3ff;
}
.ch-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 4px; }
.warn-badge {
  padding: 1px 6px; border-radius: 5px; font-size: 10px; font-weight: 500;
  background: #fef3c7; color: #92400e; border: 1px solid #fde68a;
}
.user-chip { margin-left: auto; font-size: 11.5px; color: var(--text-3); white-space: nowrap; }
select:disabled, input:disabled, textarea:disabled { opacity: 0.55; cursor: not-allowed; }

.form-title { margin: 18px 0 10px; font-size: 12px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; color: var(--text-3); }
.user-create { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.user-create input, .user-create select {
  padding: 8px 10px; border: 1px solid var(--line-2); border-radius: 9px;
  background: var(--panel-2); outline: none; font-size: 12.5px;
}
.user-create input { width: 160px; }
.user-create input:focus, .user-create select:focus { border-color: var(--accent); background: #fff; }

.users-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.users-table th {
  text-align: left; padding: 7px 8px;
  font-size: 11px; font-weight: 600; color: var(--text-3);
  letter-spacing: 0.04em; text-transform: uppercase;
  border-bottom: 1px solid var(--line);
}
.users-table td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.users-table tr:last-child td { border-bottom: none; }
.users-table select {
  padding: 4px 8px; border: 1px solid var(--line-2); border-radius: 7px;
  background: var(--panel-2); font-size: 12px;
}

.report-cards { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; }
.rcard {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 8px; text-align: center;
}
.rcard b { display: block; font-size: 16px; font-weight: 600; }
.rcard span { font-size: 10.5px; color: var(--text-2); }
.rcard.warn b { color: var(--amber); }
.rcard.info b { color: #2563eb; }
.rcard.good b { color: var(--green); }

.daily-bars { display: flex; flex-direction: column; gap: 5px; }
.drow { display: grid; grid-template-columns: 44px minmax(0, 1fr) 52px; gap: 10px; align-items: center; }
.dlabel { font-size: 11px; color: var(--text-3); font-family: var(--mono); }
.dbars { display: flex; flex-direction: column; gap: 2px; }
.dbar { height: 7px; border-radius: 4px; min-width: 2px; }
.dbar.created { background: var(--accent); }
.dbar.resolved { background: var(--green); }
.dnum { font-size: 11px; color: var(--text-2); text-align: right; font-family: var(--mono); }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px);
  padding: 10px 18px; border-radius: 10px;
  background: #18181b; color: #fff; font-size: 13px;
  opacity: 0; pointer-events: none; transition: 0.2s; z-index: 99;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #d8d8de; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #c4c4cc; }
::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 1180px) {
  .app { grid-template-columns: 64px 300px minmax(0, 1fr); }
  .side { display: none; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 56px minmax(0, 1fr); }
  .list { display: none; }
  .chat-actions .btn span { display: none; }
}
