:root {
  --bg: #f2f4f8;
  --text: #1f2a37;
  --muted: #708198;
  --card: #ffffff;
  --border: #e2e8f0;
  --primary: #4f46e5;
  --primary-soft: #eef0ff;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --sidebar: #0f172a;
  --sidebar-2: #111d36;
  --topbar: #ffffff;
  --radius: 14px;
  --shadow: 0 12px 32px rgba(16, 24, 40, 0.06);
}

* { box-sizing: border-box; }

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: "Manrope", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

body.sidebar-open {
  overflow: hidden;
}

a { color: inherit; }

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}

.auth-card {
  width: 100%;
  max-width: 460px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px;
}

.app-shell {
  min-height: 100vh;
  display: flex;
}

.sidebar {
  width: 266px;
  background: linear-gradient(180deg, var(--sidebar), var(--sidebar-2));
  color: #c6d1e7;
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
  border-right: 1px solid rgba(255, 255, 255, 0.07);
  display: flex;
  flex-direction: column;
}

.sidebar-inner {
  flex: 1;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #fff;
  text-decoration: none;
  font-size: 28px;
  font-weight: 800;
  padding: 10px 12px 16px;
  letter-spacing: -0.02em;
}

.brand-dot {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  background: linear-gradient(135deg, #8b5cf6, #ec4899);
  box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.15);
}

.nav-group {
  margin-top: 8px;
}

.nav-label {
  margin: 10px 12px 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: #7f90ab;
  font-weight: 700;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.side-link {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #d9e7f8;
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
}

.side-icon {
  width: 17px;
  height: 17px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.95;
}

.side-icon svg {
  width: 17px;
  height: 17px;
}

.side-link:hover,
.side-link.active {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.35), rgba(236, 72, 153, 0.35));
  color: #fff;
}

.main-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.sidebar-overlay {
  display: none;
}

.mobile-top-strip {
  display: none;
}

.topbar {
  background: var(--topbar);
  border-bottom: 1px solid var(--border);
  padding: 14px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.menu-toggle {
  display: none;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  padding: 9px 8px;
  cursor: pointer;
}

.menu-toggle span {
  display: block;
  height: 2px;
  background: #334155;
  border-radius: 999px;
  margin: 4px 0;
}

.top-title {
  margin: 0;
  font-size: 28px;
  line-height: 1.05;
  letter-spacing: -0.02em;
}

.top-meta {
  margin: 5px 0 0;
  font-size: 13px;
  color: var(--muted);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-end;
}

.top-search {
  min-width: 320px;
}

.top-search input {
  border-radius: 999px;
  border: 1px solid var(--border);
  padding: 10px 14px;
  background: #fafbff;
}

.top-icons {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
}

.user-pill {
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 13px;
  font-weight: 700;
}

.crm-toast-stack {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 2147483200;
  width: min(360px, calc(100vw - 24px));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.crm-toast {
  width: 100%;
  border: 0;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  text-align: left;
  padding: 14px 15px;
  border-radius: 16px;
  background: linear-gradient(135deg, #111827, #1f2937);
  color: #fff;
  box-shadow: 0 18px 45px rgba(15, 23, 42, 0.28);
  cursor: pointer;
  transform: translateY(-8px);
  opacity: 0;
  animation: crmToastIn .22s ease forwards;
}

.crm-toast.leaving {
  animation: crmToastOut .2s ease forwards;
}

.crm-toast-dot {
  width: 11px;
  height: 11px;
  border-radius: 999px;
  background: #22c55e;
  box-shadow: 0 0 0 6px rgba(34, 197, 94, 0.14);
  flex: 0 0 auto;
  margin-top: 5px;
}

.crm-toast-body {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.crm-toast-title {
  font-size: 14px;
  font-weight: 800;
  line-height: 1.2;
}

.crm-toast-text {
  font-size: 13px;
  line-height: 1.35;
  color: rgba(255, 255, 255, 0.82);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@keyframes crmToastIn {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes crmToastOut {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(-8px);
  }
}

.sidebar-user {
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  padding: 12px 10px 6px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-avatar {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  object-fit: cover;
}

.sidebar-user-meta {
  min-width: 0;
  flex: 1;
}

.sidebar-user-name {
  margin: 0;
  color: #eff5ff;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.content-wrap {
  flex: 1;
  min-height: 0;
  padding: 18px 22px 30px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.content-wrap-chats {
  overflow: hidden;
  padding-bottom: 14px;
  max-height: 90vh;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  overflow-x: auto;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.card-head h2 {
  margin: 0;
  font-size: 28px;
  letter-spacing: -0.02em;
}

.stats-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.stat-card {
  min-width: 220px;
  flex: 1;
  padding: 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
}

.stat-card .label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
}

.stat-card .value {
  margin-top: 8px;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.stat-card .meta {
  margin-top: 6px;
  font-size: 13px;
  font-weight: 600;
}

.meta.up { color: var(--ok); }
.meta.down { color: var(--danger); }
.meta.warn { color: var(--warn); }

.stack-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 820px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
}

input,
select,
textarea {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  width: 100%;
  font-family: inherit;
  background: #fff;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: #818cf8;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.check {
  display: flex;
  align-items: center;
  flex-direction: row;
  gap: 8px;
}

.permissions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  min-width: 820px;
}

thead th {
  background: #f8faff;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  color: #4b5c73;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

thead th:first-child { border-top-left-radius: 10px; }
thead th:last-child { border-top-right-radius: 10px; }

th,
td {
  padding: 11px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  font-size: 14px;
}

tbody tr:hover td {
  background: #fcfdff;
}

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.multi-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.inline-form { margin: 0; }

.btn {
  background: linear-gradient(135deg, #4f46e5, #6366f1);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 9px 13px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  font-size: 14px;
  font-weight: 700;
}

.btn:hover {
  filter: brightness(1.02);
}

.btn-small {
  padding: 6px 10px;
  font-size: 13px;
}

.btn-danger {
  background: linear-gradient(135deg, #ef4444, #dc2626);
}

.btn-ghost {
  background: #fff;
  color: #1f3045;
  border: 1px solid var(--border);
}

.btn-quiet {
  background: transparent;
  color: #9cb0cb;
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-weight: 600;
}

.btn-quiet:hover {
  color: #d7e4f8;
  border-color: rgba(255, 255, 255, 0.3);
}

.col-gap {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.flash {
  border-radius: 10px;
  padding: 11px 13px;
  font-size: 14px;
  font-weight: 600;
}

.flash.success {
  background: #e8f8ef;
  color: var(--ok);
  border: 1px solid #b6ebcc;
}

.flash.error {
  background: #fdeceb;
  color: var(--danger);
  border: 1px solid #f2c6c3;
}

.muted {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.settings-tabs {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.settings-tab {
  text-decoration: none;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: #203246;
  background: #fff;
  font-weight: 700;
}

.settings-tab.active {
  border-color: #6366f1;
  color: #3730a3;
  background: #eef0ff;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  color: #12263a;
  border: 1px solid rgba(0,0,0,0.07);
  font-weight: 700;
}

.subsection {
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #fafcff;
}

.subsection h3 {
  margin: 0;
  font-size: 16px;
}

.two-col {
  display: flex;
  gap: 10px;
}

.two-col > label { flex: 1; }

.kv-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.kv-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}

.kv-row span { color: var(--muted); }

code {
  background: #f4f7fa;
  padding: 2px 6px;
  border-radius: 6px;
}

.chat-layout {
  display: flex;
  gap: 14px;
  flex: 1;
  min-height: 0;
  height: 100%;
}

.chat-list {
  width: 360px;
  padding: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  min-height: 0;
  height: 100%;
}

.chat-list-head {
  padding: 14px 14px 10px;
  border-bottom: 1px solid var(--border);
}

.chat-list-head h2 {
  margin: 0 0 4px;
  font-size: 20px;
}

.chat-list-items {
  height: 100%;
  min-height: 0;
  overflow-y: auto;
}

.chat-thread-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px;
  text-decoration: none;
  color: inherit;
  border-bottom: 1px solid var(--border);
  background: #fff;
}

.chat-thread-item:hover {
  background: #f9fbff;
}

.chat-thread-item.active {
  background: #eef2ff;
}

.chat-avatar {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  object-fit: cover;
  flex-shrink: 0;
  border: 1px solid var(--border);
}

.chat-thread-main {
  min-width: 0;
  flex: 1;
}

.chat-thread-top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.chat-thread-name {
  margin: 0;
  font-weight: 800;
  font-size: 14px;
}

.chat-thread-time {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.chat-source-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.platform-icon {
  line-height: 1;
}

.chat-last-message {
  margin: 3px 0 0;
  font-size: 13px;
  color: #526276;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-thread-right {
  min-width: 22px;
  display: flex;
  justify-content: flex-end;
  padding-top: 1px;
}

.chat-unread-badge {
  min-width: 20px;
  height: 20px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #4f46e5;
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  padding: 0 6px;
}

.chat-unread-badge.hidden {
  visibility: hidden;
}

.chat-panel {
  flex: 1;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding: 0;
  overflow: hidden;
  min-height: 0;
  height: 100%;
}

.chat-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.chat-head-user {
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-head-name {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
}

.chat-head-meta {
  margin: 3px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.chat-messages {
  height: 100%;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f8faff;
}

.chat-bubble {
  display: flex;
}

.chat-bubble.theirs {
  justify-content: flex-start;
}

.chat-bubble.mine {
  justify-content: flex-end;
}

.chat-bubble-content {
  max-width: min(78%, 760px);
  border-radius: 14px;
  padding: 9px 12px 8px;
  border: 1px solid var(--border);
  background: #fff;
}

.chat-bubble.mine .chat-bubble-content {
  background: #4f46e5;
  border-color: #4f46e5;
  color: #fff;
}

.chat-bubble-author {
  margin: 0 0 4px;
  font-size: 12px;
  font-weight: 800;
  opacity: 0.9;
}

.chat-bubble-text {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-bubble-time {
  margin: 5px 0 0;
  font-size: 11px;
  opacity: 0.78;
  text-align: right;
}

.chat-compose {
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  padding: 10px;
  background: #fff;
}

.chat-compose input {
  flex: 1;
}

.chat-empty {
  min-height: 320px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  padding: 20px;
  text-align: center;
}

@media (max-width: 1280px) {
  .top-search { min-width: 220px; }
}

@media (max-width: 1040px) {
  .topbar { flex-direction: column; align-items: flex-start; }
  .topbar-right { width: 100%; flex-wrap: wrap; }
  .top-search { min-width: 100%; width: 100%; }
}

@media (max-width: 980px) {
  .app-shell { position: relative; }
  .menu-toggle { display: inline-block; }
  .mobile-top-strip {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding: 8px 12px;
    background: #fff;
    border-bottom: 1px solid var(--border);
  }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: 280px;
    height: 100vh;
    z-index: 120;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 110;
  }
  body.sidebar-open .sidebar-overlay {
    opacity: 1;
    pointer-events: auto;
  }
  .topbar { border-top: 0; }
  .top-search { display: none; }
  .content-wrap-chats {
    overflow: visible;
    padding-bottom: 30px;
  }
  .chat-layout { flex-direction: column; min-height: auto; height: auto; }
  .chat-list { width: 100%; }
  .chat-list-items { max-height: 280px; }
  .chat-bubble-content { max-width: 92%; }
  .two-col { flex-direction: column; }
}
