:root {
  --bg: #0b0d12;
  --bg-soft: #0f1219;
  --panel: #12151d;
  --panel-2: #171b25;
  --panel-3: #1c2130;
  --border: #232a3a;
  --border-soft: #1b2130;
  --text: #e8ecf5;
  --muted: #8d97ab;
  --muted-2: #66708a;
  --accent: #7c6cf6;
  --accent-2: #22d3ee;
  --accent-soft: rgba(124, 108, 246, 0.14);
  --green: #34d399;
  --amber: #fbbf24;
  --red: #f87171;
  --blue: #60a5fa;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --font: "Segoe UI", system-ui, -apple-system, "Inter", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: radial-gradient(1200px 700px at 80% -10%, rgba(124, 108, 246, 0.16), transparent 60%),
              radial-gradient(900px 600px at -10% 110%, rgba(34, 211, 238, 0.1), transparent 55%),
              var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
::selection { background: rgba(124, 108, 246, 0.4); }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: linear-gradient(180deg, rgba(28, 33, 48, 0.9), rgba(18, 21, 29, 0.96));
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 32px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}
.login-card::before {
  content: "";
  position: absolute;
  inset: -1px;
  background: linear-gradient(120deg, rgba(124, 108, 246, 0.5), transparent 40%, rgba(34, 211, 238, 0.35));
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
  border-radius: inherit;
  padding: 1px;
  pointer-events: none;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center;
  color: #0b0d12; font-weight: 800; font-size: 19px;
  box-shadow: 0 8px 24px rgba(124, 108, 246, 0.35);
}
.brand-name { font-weight: 650; letter-spacing: 0.2px; }
.brand-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

.login-card h1 { font-size: 22px; margin: 28px 0 6px; font-weight: 650; }
.login-card .hint { color: var(--muted); margin: 0 0 22px; line-height: 1.5; }

/* ---------- layout ---------- */
.shell {
  display: grid;
  grid-template-columns: 244px 1fr;
  min-height: 100vh;
}
.sidebar {
  background: linear-gradient(180deg, rgba(18, 21, 29, 0.98), rgba(13, 15, 22, 0.98));
  border-right: 1px solid var(--border-soft);
  padding: 22px 14px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar .brand { padding: 0 8px; }
.nav { display: flex; flex-direction: column; gap: 3px; }
.nav-label {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.12em;
  color: var(--muted-2); padding: 14px 10px 6px;
}
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 9px; color: var(--muted);
  font-weight: 520; transition: 0.15s ease;
}
.nav a:hover { background: rgba(255, 255, 255, 0.04); color: var(--text); }
.nav a.active {
  background: linear-gradient(90deg, rgba(124, 108, 246, 0.2), rgba(124, 108, 246, 0.05));
  color: #fff;
  box-shadow: inset 2px 0 0 var(--accent);
}
.nav a .ico { width: 18px; height: 18px; flex: none; opacity: 0.9; }
.nav a .count {
  margin-left: auto; background: var(--accent); color: #fff;
  font-size: 10.5px; border-radius: 999px; padding: 1px 7px;
}
.sidebar-foot { margin-top: auto; }
.user-card {
  display: flex; align-items: center; gap: 10px;
  padding: 10px; border-radius: 12px; background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border-soft);
}
.avatar {
  width: 34px; height: 34px; border-radius: 50%; flex: none;
  background: linear-gradient(135deg, rgba(124, 108, 246, 0.9), rgba(34, 211, 238, 0.8));
  display: grid; place-items: center; font-weight: 700; color: #0b0d12; font-size: 13px;
}
.user-meta { min-width: 0; }
.user-meta .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-meta .role { color: var(--muted-2); font-size: 11.5px; }
.icon-btn {
  background: transparent; border: 0; color: var(--muted); cursor: pointer;
  padding: 7px; border-radius: 8px; display: inline-grid; place-items: center;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.06); color: var(--text); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 20px;
  padding: 26px 32px 18px;
}
.topbar h1 { margin: 0; font-size: 24px; font-weight: 650; letter-spacing: -0.02em; }
.topbar p { margin: 5px 0 0; color: var(--muted); }
.topbar-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.view { padding: 0 32px 40px; flex: 1; min-width: 0; }
.view.flush { padding: 0 32px 24px; }

/* ---------- primitives ---------- */
.card {
  background: linear-gradient(180deg, rgba(24, 28, 40, 0.86), rgba(18, 21, 29, 0.92));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-title { font-weight: 620; font-size: 14.5px; }
.card-sub { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 12px; }
.nowrap { white-space: nowrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-weight: 560; cursor: pointer;
  transition: 0.15s ease; white-space: nowrap;
}
.btn:hover { background: var(--panel-3); border-color: #2e374b; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(135deg, var(--accent), #5b4ce0);
  border-color: transparent; color: #fff;
  box-shadow: 0 8px 22px rgba(124, 108, 246, 0.28);
}
.btn-primary:hover { background: linear-gradient(135deg, #8b7cff, #6857f0); }
.btn-cyan { background: linear-gradient(135deg, #0ea5c4, #0891b2); border-color: transparent; color: #fff; }
.btn-ghost { background: transparent; }
.btn-danger { background: rgba(248, 113, 113, 0.12); border-color: rgba(248, 113, 113, 0.35); color: #fca5a5; }
.btn-sm { padding: 6px 10px; font-size: 12.5px; border-radius: 8px; }
.btn-block { width: 100%; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 12.5px; color: var(--muted); font-weight: 550; }
.input, .select, .textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: rgba(9, 11, 16, 0.7);
  color: var(--text); outline: none; transition: 0.15s;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.textarea { min-height: 90px; resize: vertical; line-height: 1.55; }
.select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 34px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.form-grid .full { grid-column: 1 / -1; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 600;
  background: rgba(255, 255, 255, 0.06); color: var(--muted); border: 1px solid transparent;
}
.badge.green { background: rgba(52, 211, 153, 0.12); color: #6ee7b7; border-color: rgba(52, 211, 153, 0.25); }
.badge.amber { background: rgba(251, 191, 36, 0.12); color: #fcd34d; border-color: rgba(251, 191, 36, 0.25); }
.badge.red { background: rgba(248, 113, 113, 0.12); color: #fca5a5; border-color: rgba(248, 113, 113, 0.25); }
.badge.blue { background: rgba(96, 165, 250, 0.12); color: #93c5fd; border-color: rgba(96, 165, 250, 0.25); }
.badge.violet { background: rgba(124, 108, 246, 0.16); color: #c4b5fd; border-color: rgba(124, 108, 246, 0.3); }
.dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

.stat { padding: 16px 18px; }
.stat .label { color: var(--muted); font-size: 12.5px; display: flex; align-items: center; gap: 8px; }
.stat .value { font-size: 27px; font-weight: 680; margin-top: 8px; letter-spacing: -0.02em; }
.stat .sub { color: var(--muted-2); font-size: 12px; margin-top: 4px; }
.stat .value.green { color: #6ee7b7; }
.stat .value.amber { color: #fcd34d; }
.stat .value.red { color: #fca5a5; }

.table-wrap { overflow: auto; border-radius: var(--radius); border: 1px solid var(--border); }
table.data { width: 100%; border-collapse: collapse; background: rgba(18, 21, 29, 0.7); }
table.data th, table.data td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
table.data th { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted-2); font-weight: 600; background: rgba(9, 11, 16, 0.45); position: sticky; top: 0; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: rgba(255, 255, 255, 0.025); }
table.data td.actions { text-align: right; white-space: nowrap; }

.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty .big { font-size: 30px; margin-bottom: 8px; opacity: 0.7; }

/* ---------- chats ---------- */
.chats-layout {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  gap: 16px;
  height: calc(100vh - 130px);
  min-height: 520px;
}
.chat-list-card {
  display: flex; flex-direction: column; padding: 0; overflow: hidden;
}
.chat-list-head { padding: 14px; border-bottom: 1px solid var(--border-soft); display: flex; flex-direction: column; gap: 10px; }
.chat-filters { display: flex; gap: 5px; background: rgba(9, 11, 16, 0.5); padding: 4px; border-radius: 9px; }
.chat-filters button {
  flex: 1; background: transparent; border: 0; padding: 6px 8px; border-radius: 7px;
  color: var(--muted); cursor: pointer; font-size: 12px; font-weight: 550;
}
.chat-filters button.active { background: var(--panel-3); color: var(--text); }
.chat-list { overflow-y: auto; flex: 1; }
.chat-item {
  display: block; padding: 12px 14px; border-bottom: 1px solid var(--border-soft);
  cursor: pointer; transition: 0.12s;
}
.chat-item:hover { background: rgba(255, 255, 255, 0.03); }
.chat-item.active { background: linear-gradient(90deg, rgba(124, 108, 246, 0.16), rgba(124, 108, 246, 0.03)); box-shadow: inset 2px 0 0 var(--accent); }
.chat-item-top { display: flex; align-items: center; gap: 8px; }
.chat-item-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { margin-left: auto; color: var(--muted-2); font-size: 11px; flex: none; }
.chat-item-preview { color: var(--muted); font-size: 12.5px; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-meta { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.chat-item-meta .channel { font-size: 11px; color: var(--muted-2); }
.unread-pill { background: var(--accent); color: #fff; border-radius: 999px; font-size: 10.5px; padding: 1px 7px; margin-left: auto; }

.chat-panel { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.chat-head { padding: 14px 18px; border-bottom: 1px solid var(--border-soft); display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.chat-head .who { font-weight: 620; }
.chat-head .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.chat-head-actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.msg-row { display: flex; }
.msg-row.outbound { justify-content: flex-end; }
.msg {
  max-width: min(72%, 620px); padding: 10px 14px; border-radius: 14px;
  background: var(--panel-3); border: 1px solid var(--border-soft); line-height: 1.5;
  white-space: pre-wrap; word-break: break-word;
}
.msg-row.inbound .msg { border-bottom-left-radius: 5px; }
.msg-row.outbound .msg {
  background: linear-gradient(135deg, rgba(124, 108, 246, 0.9), rgba(91, 76, 224, 0.9));
  border-color: transparent; border-bottom-right-radius: 5px; color: #fff;
}
.msg-foot { display: flex; gap: 8px; align-items: center; margin-top: 4px; font-size: 10.5px; color: var(--muted-2); }
.msg-row.outbound .msg-foot { color: rgba(255, 255, 255, 0.65); }
.only-mobile { display: none; }
.draft-box {
  margin: 0 18px 10px; padding: 12px 14px; border-radius: 12px;
  background: linear-gradient(135deg, rgba(124, 108, 246, 0.13), rgba(34, 211, 238, 0.08));
  border: 1px solid rgba(124, 108, 246, 0.35);
}
.draft-box .draft-title { display: flex; align-items: center; gap: 8px; font-size: 11.5px; color: #c4b5fd; font-weight: 650; text-transform: uppercase; letter-spacing: 0.05em; }
.draft-box .draft-text { margin: 8px 0 10px; line-height: 1.5; white-space: pre-wrap; }
.composer { padding: 12px 18px 16px; border-top: 1px solid var(--border-soft); }
.composer textarea {
  width: 100%; min-height: 74px; max-height: 200px; resize: none; padding: 12px 14px;
  border-radius: 12px; border: 1px solid var(--border); background: rgba(9, 11, 16, 0.7);
  color: var(--text); outline: none; line-height: 1.5;
}
.composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer-foot { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.composer-hint { color: var(--muted-2); font-size: 11.5px; }
.chat-empty { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 30px; }

/* ---------- modal / toast ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(4, 6, 10, 0.7); backdrop-filter: blur(4px);
  display: grid; place-items: center; padding: 20px; z-index: 50;
}
.modal {
  width: 100%; max-width: 520px; max-height: 90vh; overflow: auto;
  background: linear-gradient(180deg, #171b25, #12151d);
  border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow);
}
.modal.wide { max-width: 760px; }
.modal-head { padding: 20px 22px 0; display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.modal-head h3 { margin: 0; font-size: 17px; font-weight: 650; }
.modal-head p { margin: 6px 0 0; color: var(--muted); font-size: 12.5px; }
.modal-body { padding: 18px 22px; }
.modal-foot { padding: 0 22px 20px; display: flex; justify-content: flex-end; gap: 10px; }

.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 100; }
.toast {
  min-width: 240px; max-width: 380px; padding: 12px 15px; border-radius: 11px;
  background: #1b2130; border: 1px solid var(--border); box-shadow: var(--shadow);
  animation: slide-in 0.18s ease;
}
.toast.error { border-color: rgba(248, 113, 113, 0.4); background: #241a1e; }
.toast.success { border-color: rgba(52, 211, 153, 0.35); }
@keyframes slide-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

.spinner {
  width: 15px; height: 15px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25); border-top-color: #fff;
  animation: spin 0.7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { background: linear-gradient(90deg, rgba(255,255,255,0.04), rgba(255,255,255,0.08), rgba(255,255,255,0.04)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 8px; }
@keyframes shimmer { to { background-position: -200% 0; } }

.prompt-preview {
  color: var(--muted); font-size: 12.5px; margin-top: 6px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.ai-mode { display: flex; gap: 4px; background: rgba(9, 11, 16, 0.5); padding: 3px; border-radius: 8px; }
.ai-mode button { border: 0; background: transparent; color: var(--muted); padding: 5px 8px; border-radius: 6px; cursor: pointer; font-size: 11.5px; }
.ai-mode button.active { background: var(--panel-3); color: var(--text); }
.integration-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border-soft); }
.integration-row:last-child { border-bottom: 0; }

@media (max-width: 1100px) {
  .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: static; height: auto; flex-direction: row; align-items: center;
    padding: 10px 12px; gap: 12px; overflow-x: auto;
  }
  .sidebar .brand-name, .sidebar .brand-sub, .nav-label, .sidebar-foot .user-meta { display: none; }
  .nav { flex-direction: row; gap: 4px; }
  .nav a { padding: 8px 10px; }
  .nav a span.label { display: none; }
  .sidebar-foot { margin: 0 0 0 auto; }
  .topbar { padding: 18px 16px 12px; }
  .view, .view.flush { padding: 0 16px 24px; }
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: minmax(0, 1fr); }
  .chats-layout { grid-template-columns: minmax(0, 1fr); height: calc(100vh - 200px); }
  body.chat-open .chat-list-card { display: none; }
  body:not(.chat-open) .chat-panel { display: none; }
  .msg { max-width: 88%; }
  .form-grid { grid-template-columns: 1fr; }
  .only-mobile { display: inline-flex; }
}
