:root {
  --bg: #FAF7F2;
  --surface: #FFFFFF;
  --surface-alt: #F4EFE8;
  --border: #E8E2D9;
  --ink: #26201A;
  --muted: #6B6054;
  --accent: #E43C21;
  --accent-hover: #C7311A;
  --accent-soft: #FFF2F0;
  --green: #1F8A5B;
  --green-soft: #ECFDF5;
  --blue: #2563EB;
  --amber: #D97706;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(38, 32, 26, 0.05);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
.mono { font-family: 'JetBrains Mono', ui-monospace, monospace; font-size: 12px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.full { width: 100%; }

/* Buttons & Inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 9px;
  border: 1px solid transparent;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.14s ease;
  font-family: inherit;
}
.btn-sm { padding: 6px 11px; font-size: 12.5px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-outline { background: var(--surface); border-color: var(--border); color: var(--ink); }
.btn-outline:hover { border-color: #CFC5B7; background: var(--surface-alt); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-alt); color: var(--ink); }
.btn-success { background: var(--green); color: #fff; }
.btn-danger-outline { background: #fff; border-color: #FECACA; color: #B91C1C; }
.btn-danger-outline:hover { background: #FEF2F2; }
.btn-icon {
  background: transparent; border: none; font-size: 16px; color: var(--muted);
  cursor: pointer; padding: 4px 8px; border-radius: 6px;
}
.btn-icon:hover { background: var(--surface-alt); color: var(--ink); }

input, select, textarea {
  font-family: inherit;
  font-size: 13.5px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 11px;
  outline: none;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(228, 60, 33, 0.12);
}

/* Auth Gate */
.auth-gate {
  position: fixed; inset: 0; background: var(--bg); z-index: 999;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.auth-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.auth-card .brand-mark { margin: 0 auto; }
.form-error { color: #B91C1C; font-size: 12.5px; min-height: 16px; }

/* Shell */
.app-shell { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 10px 18px; background: var(--surface); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; background: var(--accent); color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 17px; flex-shrink: 0;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-size: 14.5px; }
.brand-sub { font-size: 11px; color: var(--muted); }
.ws-picker { display: flex; align-items: center; gap: 6px; padding-left: 10px; border-left: 1px solid var(--border); }
.ws-picker select { font-weight: 600; max-width: 220px; padding: 6px 10px; }

.nav-tabs { display: flex; gap: 4px; background: var(--surface-alt); padding: 4px; border-radius: 11px; }
.nav-tab {
  border: none; background: transparent; padding: 7px 13px; border-radius: 8px;
  font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer;
  transition: all 0.14s ease; font-family: inherit;
}
.nav-tab:hover { color: var(--ink); }
.nav-tab.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }

.main-area { flex: 1; min-height: 0; overflow: hidden; position: relative; }
.view { display: none; height: 100%; }
.view.active { display: flex; }

/* VIEW 1: INBOX 3-COLUMN */
.inbox-layout { flex-direction: row; }
.inbox-list-pane {
  width: 315px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.pane-header { padding: 12px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.search-input { width: 100%; }
.filter-pills { display: flex; gap: 5px; overflow-x: auto; padding-bottom: 2px; }
.filter-pill {
  flex-shrink: 0; font-size: 11.5px; font-weight: 600; padding: 4px 9px; border-radius: 99px;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted); cursor: pointer;
}
.filter-pill.active { background: var(--ink); color: #fff; border-color: var(--ink); }

.lead-list { flex: 1; overflow-y: auto; }
.lead-item {
  padding: 12px 14px; border-bottom: 1px solid #F1ECE4; cursor: pointer;
  transition: background 0.12s ease; display: flex; flex-direction: column; gap: 5px;
}
.lead-item:hover { background: #FCF9F5; }
.lead-item.selected { background: var(--accent-soft); border-left: 3px solid var(--accent); padding-left: 11px; }
.lead-item-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.lead-item-name { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead-item-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.lead-item-preview {
  font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.lead-item-tags { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.tag-chip {
  font-size: 10.5px; font-weight: 600; padding: 2px 7px; border-radius: 99px;
  display: inline-flex; align-items: center; gap: 4px;
}
.unread-dot {
  background: var(--accent); color: #fff; font-size: 10.5px; font-weight: 700;
  padding: 1px 6px; border-radius: 99px;
}

/* CHAT PANE */
.chat-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
.chat-active { display: flex; flex-direction: column; height: 100%; }
.chat-header {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 11px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.chat-header-info { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%; background: var(--surface-alt);
  border: 1px solid var(--border); display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: var(--accent); flex-shrink: 0;
}
.chat-title-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.chat-title-row h2 { margin: 0; font-size: 15px; }
.chat-meta-row { display: flex; align-items: center; gap: 8px; margin-top: 2px; color: var(--muted); }
.stage-pill {
  font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; color: #fff;
}
.window-pill {
  font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 6px;
  background: var(--green-soft); color: var(--green);
}

.ad-banner {
  padding: 8px 16px; background: #FFF8EC; border-bottom: 1px solid #F3E2C0;
  font-size: 12px; color: #7C4A03; display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.chat-messages {
  flex: 1; overflow-y: auto; padding: 18px 20px; display: flex; flex-direction: column; gap: 10px;
}
.msg-row { display: flex; flex-direction: column; max-width: 76%; }
.msg-row.in { align-self: flex-start; }
.msg-row.out { align-self: flex-end; }
.msg-bubble {
  padding: 10px 13px; border-radius: 14px; font-size: 13.5px; line-height: 1.45;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04); white-space: pre-wrap;
}
.msg-row.in .msg-bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg-row.out.ai .msg-bubble { background: #EEF4FF; border: 1px solid #D6E4FF; border-bottom-right-radius: 4px; }
.msg-row.out.human .msg-bubble { background: #E9F7EF; border: 1px solid #C6EAD5; border-bottom-right-radius: 4px; }
.msg-row.out.system .msg-bubble { background: #FFF8EB; border: 1px solid #FDE68A; border-bottom-right-radius: 4px; }
.msg-meta { font-size: 10.5px; color: var(--muted); margin-top: 3px; display: flex; gap: 6px; }
.msg-row.out .msg-meta { justify-content: flex-end; }

.chat-composer {
  background: var(--surface); border-top: 1px solid var(--border); padding: 10px 14px;
  display: flex; flex-direction: column; gap: 8px;
}
.sim-role-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  font-size: 12px; background: var(--surface-alt); padding: 6px 10px; border-radius: 8px;
}
.role-toggle { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; font-weight: 500; }
.composer-row { display: flex; gap: 8px; }
.composer-row input { flex: 1; }

/* DETAIL SIDEBAR (RIGHT) */
.detail-pane {
  width: 340px; flex-shrink: 0; background: var(--surface); border-left: 1px solid var(--border);
  overflow-y: auto; padding: 14px;
}
.detail-section {
  padding-bottom: 13px; margin-bottom: 13px; border-bottom: 1px solid #F1ECE4;
  display: flex; flex-direction: column; gap: 8px;
}
.detail-section:last-child { border-bottom: none; margin-bottom: 0; }
.detail-section h4 {
  margin: 0; font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
}
.ai-summary-box {
  background: #F7F9FF; border: 1px solid #DCE7FF; border-radius: 9px; padding: 9px 11px;
  font-size: 12.5px; line-height: 1.45; color: #1E3A8A;
}
.stage-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.stage-btn {
  text-align: left; padding: 7px 9px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); font-size: 12px; font-weight: 600; cursor: pointer;
  display: flex; align-items: center; gap: 6px; font-family: inherit;
}
.stage-btn .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.stage-btn.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.label-toggles { display: flex; flex-wrap: wrap; gap: 6px; }
.label-toggle-btn {
  border: 1px solid var(--border); border-radius: 99px; padding: 4px 9px;
  font-size: 11.5px; font-weight: 600; cursor: pointer; background: var(--surface); color: var(--muted);
}
.label-toggle-btn.active { border-color: currentColor; }
.capi-log-item {
  background: var(--surface-alt); border-radius: 8px; padding: 8px 10px; font-size: 12px;
  display: flex; flex-direction: column; gap: 3px;
}

/* VIEW 2: KANBAN */
.kanban-view { flex-direction: column; padding: 18px 20px; overflow: hidden; }
.view-toolbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.view-toolbar h2 { margin: 0 0 3px; font-size: 18px; }
.view-toolbar p { margin: 0; font-size: 13px; }
.kanban-board {
  flex: 1; display: flex; gap: 12px; overflow-x: auto; padding-bottom: 8px; align-items: stretch;
}
.kanban-col {
  width: 275px; flex-shrink: 0; background: var(--surface-alt); border: 1px solid var(--border);
  border-radius: 13px; display: flex; flex-direction: column; max-height: 100%;
}
.kanban-col.drag-over { border-color: var(--accent); background: var(--accent-soft); }
.kanban-col-header {
  padding: 11px 12px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.kanban-col-title { display: flex; align-items: center; gap: 7px; font-weight: 700; font-size: 13px; }
.kanban-col-title .dot { width: 9px; height: 9px; border-radius: 50%; }
.kanban-col-count { font-size: 11.5px; font-weight: 700; color: var(--muted); background: var(--surface); padding: 2px 7px; border-radius: 99px; }
.kanban-cards { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 9px; }
.kanban-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 11px; box-shadow: var(--shadow); cursor: grab; display: flex; flex-direction: column; gap: 6px;
}
.kanban-card:active { cursor: grabbing; }
.kanban-card-name { font-weight: 600; font-size: 13.5px; display: flex; justify-content: space-between; gap: 6px; }
.kanban-card-ad { font-size: 11.5px; color: #92400E; background: #FFF8EC; padding: 3px 7px; border-radius: 6px; }
.kanban-card-footer { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; font-size: 12px; }

/* VIEW 3: ANALYTICS */
.analytics-view { flex-direction: column; padding: 18px 22px; overflow-y: auto; gap: 16px; }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(175px, 1fr)); gap: 12px; }
.kpi-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.kpi-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.kpi-value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.kpi-sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.analytics-grid { display: grid; grid-template-columns: 1fr 1.25fr; gap: 14px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 12px; font-size: 15px; }
.funnel-bars { display: flex; flex-direction: column; gap: 10px; }
.funnel-row { display: flex; flex-direction: column; gap: 4px; }
.funnel-row-head { display: flex; justify-content: space-between; font-size: 13px; font-weight: 600; }
.funnel-track { height: 22px; background: var(--surface-alt); border-radius: 7px; overflow: hidden; }
.funnel-fill { height: 100%; border-radius: 7px; transition: width 0.3s ease; min-width: 6px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid #F1ECE4; }
th { font-size: 11.5px; text-transform: uppercase; color: var(--muted); font-weight: 600; }

/* VIEW 4: SETTINGS */
.settings-view { flex-direction: column; padding: 18px 22px; overflow-y: auto; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); gap: 16px; max-width: 1200px; }
.form-stack { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--ink); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.code-box {
  background: #26201A; color: #FAF7F2; padding: 9px 12px; border-radius: 8px;
  font-family: 'JetBrains Mono', monospace; font-size: 12px; word-break: break-all;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}

/* Empty States & Modals & Toasts */
.empty-state {
  margin: auto; text-align: center; padding: 32px; max-width: 360px; color: var(--muted);
}
.empty-icon { font-size: 38px; margin-bottom: 8px; }
.modal {
  position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-backdrop { position: absolute; inset: 0; background: rgba(38, 32, 26, 0.45); }
.modal-card {
  position: relative; width: 100%; max-width: 500px; background: var(--surface);
  border-radius: 16px; padding: 20px 22px; box-shadow: 0 20px 50px rgba(0,0,0,0.2);
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.toast-container {
  position: fixed; bottom: 20px; right: 20px; z-index: 400; display: flex; flex-direction: column; gap: 8px;
}
.toast {
  background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 10px;
  font-size: 13px; font-weight: 500; box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}
.toast.ok { background: var(--green); }
.toast.err { background: #B91C1C; }

.mobile-only { display: none; }
.mobile-bottom-nav { display: none; }

@media (max-width: 1024px) {
  .detail-pane { width: 290px; }
  .analytics-grid { grid-template-columns: 1fr; }
}

@media (max-width: 820px) {
  .nav-tabs { display: none; }
  .brand-sub { display: none; }
  .mobile-only { display: inline-flex; }
  .mobile-bottom-nav {
    display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface);
    border-top: 1px solid var(--border); padding: 4px;
  }
  .mobile-bottom-nav .nav-tab {
    display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 16px; padding: 6px 4px;
  }
  .mobile-bottom-nav .nav-tab span { font-size: 11px; }
  .inbox-list-pane { width: 100%; }
  .chat-pane, .detail-pane { display: none; width: 100%; }
  .inbox-layout.mobile-chat .inbox-list-pane { display: none; }
  .inbox-layout.mobile-chat .chat-pane { display: flex; }
  .inbox-layout.mobile-detail .inbox-list-pane, .inbox-layout.mobile-detail .chat-pane { display: none; }
  .inbox-layout.mobile-detail .detail-pane { display: block; }
  .field-row { grid-template-columns: 1fr; }
}
