:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --line: #d8dee4;
  --text: #18211f;
  --muted: #66716f;
  --green: #1f8f5f;
  --green-dark: #176c49;
  --blue: #2f6f9f;
  --amber: #c57f28;
  --red: #bd4141;
  --shadow: 0 12px 32px rgba(20, 31, 28, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  outline: none;
}

input,
select {
  height: 42px;
  padding: 0 12px;
}

textarea {
  min-height: 96px;
  padding: 10px 12px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(31, 143, 95, 0.14);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.1;
}

h2 {
  font-size: 16px;
  line-height: 1.2;
}

[hidden] {
  display: none !important;
}

.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-panel {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.auth-panel label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.brand-mark {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--green);
  color: #fff;
  font-weight: 800;
}

.brand-mark.compact {
  width: 40px;
  height: 40px;
  font-size: 13px;
}

.error-text {
  min-height: 18px;
  color: var(--red);
  font-size: 13px;
}

.app-shell {
  height: 100vh;
  display: grid;
  grid-template-rows: 64px 1fr;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.topbar-title,
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.topbar-title span,
.chat-header span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.topbar-actions {
  color: var(--muted);
}

.view-tabs {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eef2f4;
}

.view-tab {
  min-height: 32px;
  padding: 0 12px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
}

.view-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(20, 31, 28, 0.1);
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(360px, 1fr) minmax(280px, 340px);
}

.workspace.crm-mode {
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
}

.workspace.crm-mode .chat-area,
.workspace.crm-mode .rightbar {
  display: none;
}

.sidebar,
.rightbar {
  min-height: 0;
  display: grid;
  gap: 12px;
  padding: 12px;
  overflow: auto;
}

.sidebar {
  border-right: 1px solid var(--line);
}

.rightbar {
  border-left: 1px solid var(--line);
}

.panel {
  display: grid;
  gap: 12px;
  align-content: start;
}

.panel.grow {
  min-height: 0;
}

.section-heading {
  min-height: 34px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.compact-heading {
  min-height: 24px;
}

.inline-form,
.composer,
.time-row {
  display: grid;
  gap: 8px;
}

.inline-form {
  grid-template-columns: 1fr 42px;
}

.icon-button {
  width: 42px;
  height: 42px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 42px;
  border-radius: 8px;
  background: var(--text);
  color: #fff;
}

.icon-button svg {
  width: 21px;
  height: 21px;
  fill: currentColor;
}

.icon-button.accent {
  background: var(--green);
}

.icon-button.subtle {
  background: #e8edf1;
  color: var(--text);
}

.primary-action {
  min-height: 42px;
  padding: 0 16px;
  border-radius: 8px;
  background: var(--green);
  color: #fff;
  font-weight: 700;
}

.primary-action:hover,
.icon-button.accent:hover {
  background: var(--green-dark);
}

.stack-list,
.chat-list {
  display: grid;
  gap: 8px;
}

.session-card,
.chat-card,
.rule-card,
.user-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.session-card,
.rule-card,
.user-card {
  padding: 10px;
}

.session-card {
  display: grid;
  gap: 10px;
}

.session-top,
.card-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.session-name,
.chat-name,
.rule-name,
.user-name {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-phone,
.chat-preview,
.rule-meta,
.user-meta {
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-badge {
  flex: 0 0 auto;
  border-radius: 999px;
  padding: 4px 8px;
  background: #e8edf1;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.status-badge.connected {
  background: rgba(31, 143, 95, 0.12);
  color: var(--green-dark);
}

.status-badge.qr,
.status-badge.connecting {
  background: rgba(197, 127, 40, 0.14);
  color: var(--amber);
}

.mini-button {
  min-height: 34px;
  padding: 0 10px;
  border-radius: 8px;
  background: #e8edf1;
  color: var(--text);
  font-weight: 700;
}

.mini-button.danger {
  color: var(--red);
}

.chat-card {
  min-height: 70px;
  padding: 10px;
  display: grid;
  gap: 6px;
  text-align: left;
  color: inherit;
}

.chat-card.active {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(31, 143, 95, 0.12);
}

.chat-card-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}

.unread {
  min-width: 22px;
  height: 22px;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  background: var(--blue);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
}

.chat-area {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 72px 1fr auto;
  background: #edf2f1;
}

.chat-header {
  display: flex;
  align-items: center;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.crm-area {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  background: #eef3f3;
}

.crm-header {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.crm-header span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.crm-tag-form {
  width: min(560px, 100%);
  display: grid;
  grid-template-columns: minmax(160px, 1fr) minmax(110px, 140px) auto;
  gap: 8px;
}

.crm-board {
  min-height: 0;
  display: grid;
  grid-auto-columns: minmax(250px, 300px);
  grid-auto-flow: column;
  gap: 12px;
  padding: 14px;
  overflow: auto;
}

.crm-column {
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
}

.crm-column.drag-over {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(31, 143, 95, 0.12);
}

.crm-column-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid var(--line);
}

.crm-column-title {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
}

.crm-dot {
  width: 10px;
  height: 10px;
  flex: 0 0 10px;
  border-radius: 999px;
  background: var(--muted);
}

.crm-count {
  min-width: 26px;
  height: 24px;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  background: #e8edf1;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.crm-column-list {
  min-height: 120px;
  display: grid;
  align-content: start;
  gap: 8px;
  padding: 10px;
  overflow: auto;
}

.crm-card {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: inherit;
  text-align: left;
  box-shadow: 0 3px 10px rgba(20, 31, 28, 0.06);
}

.crm-card:hover {
  border-color: rgba(31, 143, 95, 0.55);
}

.crm-card.dragging {
  opacity: 0.52;
}

.crm-card-title,
.crm-card-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.crm-card-title {
  font-weight: 800;
}

.crm-card-preview,
.crm-card-meta {
  color: var(--muted);
  font-size: 12px;
}

.crm-card-bottom {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px;
}

.crm-tag-select {
  height: 34px;
  min-width: 0;
  padding: 0 8px;
  font-size: 12px;
}

.message-list {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 18px;
  overflow: auto;
}

.message-bubble {
  max-width: min(72%, 620px);
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  word-break: break-word;
}

.message-bubble.mine {
  align-self: flex-end;
  background: #dff2e8;
  border-color: #b7ddc9;
}

.message-meta {
  margin-top: 6px;
  color: var(--muted);
  font-size: 11px;
}

.composer {
  grid-template-columns: minmax(180px, 260px) 1fr auto;
  padding: 12px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.send-action {
  min-width: 94px;
}

.rule-form,
.user-form,
.ai-form {
  display: grid;
  gap: 8px;
}

.switch-row {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.switch-row input {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  accent-color: var(--green);
}

.muted-text {
  min-height: 18px;
  color: var(--muted);
  font-size: 12px;
}

.ai-suggestion-list {
  display: grid;
  gap: 8px;
}

.ai-knowledge-list {
  display: grid;
  gap: 8px;
  max-height: 360px;
  overflow: auto;
}

.ai-suggestion-card {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.ai-knowledge-card {
  display: grid;
  gap: 6px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.ai-suggestion-question,
.ai-knowledge-question {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.ai-knowledge-answer {
  color: var(--text);
  font-size: 13px;
  line-height: 1.35;
}

.ai-knowledge-meta {
  color: var(--muted);
  font-size: 11px;
}

.ai-suggestion-card textarea {
  min-height: 86px;
  font-size: 13px;
}

.ai-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.time-row {
  grid-template-columns: 1fr 1fr;
}

.rule-card,
.user-card {
  display: grid;
  gap: 8px;
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(9, 18, 16, 0.54);
  z-index: 10;
}

.modal-panel {
  width: min(360px, 100%);
  display: grid;
  gap: 16px;
  padding: 18px;
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.modal-panel img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.modal-panel p {
  color: var(--muted);
  font-size: 13px;
}

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: min(360px, calc(100vw - 32px));
  padding: 12px 14px;
  border-radius: 8px;
  background: var(--text);
  color: #fff;
  box-shadow: var(--shadow);
  z-index: 20;
}

.empty-state {
  min-height: 72px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
  padding: 12px;
}

@media (max-width: 1080px) {
  .workspace {
    grid-template-columns: minmax(260px, 320px) 1fr;
  }

  .rightbar {
    display: none;
  }
}

@media (max-width: 760px) {
  .app-shell {
    height: auto;
    min-height: 100vh;
  }

  .topbar {
    min-height: 64px;
    align-items: flex-start;
    flex-direction: column;
    padding: 10px 12px;
  }

  .workspace {
    display: grid;
    grid-template-columns: 1fr;
  }

  .workspace.crm-mode {
    grid-template-columns: 1fr;
  }

  .sidebar,
  .rightbar {
    border: 0;
  }

  .rightbar {
    display: grid;
  }

  .chat-area {
    min-height: 620px;
  }

  .composer {
    grid-template-columns: 1fr;
  }

  .crm-header {
    align-items: stretch;
    flex-direction: column;
  }

  .crm-tag-form {
    grid-template-columns: 1fr;
  }

  .message-bubble {
    max-width: 90%;
  }
}
