:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e3e8f0;
  --text: #16202f;
  --muted: #6b7a90;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --warn: #d97706;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 8px 24px rgba(16, 24, 40, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

h2 { margin: 0 0 4px; font-size: 22px; }
h3 { margin: 0 0 12px; font-size: 16px; }
h4 { margin: 20px 0 8px; font-size: 14px; }
p { margin: 0 0 8px; }
code { background: var(--surface-alt); padding: 2px 6px; border-radius: 6px; font-size: 12px; }
pre { background: var(--surface-alt); padding: 12px; border-radius: 8px; overflow: auto; font-size: 12px; max-height: 420px; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.req { color: var(--danger); }
.link { color: var(--primary); text-decoration: none; font-weight: 600; }
.link:hover { text-decoration: underline; }
.ok-text { color: var(--success); font-weight: 600; }
.warn-text { color: var(--warn); font-weight: 600; }
.error-text { color: var(--danger); display: block; }
.overdue { color: var(--danger); font-weight: 600; }

/* Layout ------------------------------------------------------------------ */

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

.sidebar {
  width: 248px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-header { padding: 0 8px 20px; }
.brand { font-size: 17px; font-weight: 700; }
.sidebar-subtitle { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-link {
  display: flex; align-items: center; gap: 10px;
  border: none; background: none; cursor: pointer;
  padding: 9px 12px; border-radius: 8px;
  font: inherit; color: var(--muted); text-align: left; width: 100%;
}
.nav-link:hover { background: var(--surface-alt); color: var(--text); }
.nav-link.active { background: #eef2ff; color: var(--primary); font-weight: 600; }
.nav-icon { width: 18px; text-align: center; }
.sidebar-footer { border-top: 1px solid var(--border); padding-top: 14px; display: grid; gap: 10px; }
.user-pill { display: flex; flex-direction: column; padding: 0 4px; }
.user-pill span { color: var(--muted); font-size: 12px; }

.content-area { flex: 1; padding: 24px 28px 60px; display: grid; gap: 18px; align-content: start; min-width: 0; }

.panel-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 1000px) { .grid-2 { grid-template-columns: 1fr; } }

.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-header p { color: var(--muted); margin: 0; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }

/* Login ------------------------------------------------------------------- */

.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.form-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 32px; width: 100%; max-width: 400px; box-shadow: var(--shadow); }
.form-card p { color: var(--muted); margin-bottom: 20px; }
.brand-mark { font-weight: 700; color: var(--primary); margin-bottom: 18px; letter-spacing: 0.02em; }
.loading-card { padding: 40px; text-align: center; color: var(--muted); }

/* Buttons ----------------------------------------------------------------- */

.button {
  border: 1px solid transparent; border-radius: 8px; padding: 8px 14px;
  font: inherit; font-weight: 600; cursor: pointer; background: var(--surface-alt); color: var(--text);
  transition: background 0.12s ease, border-color 0.12s ease;
}
.button:hover { background: #eef1f6; }
.button:disabled { opacity: 0.5; cursor: not-allowed; }
.button-primary { background: var(--primary); color: #fff; }
.button-primary:hover { background: var(--primary-dark); }
.button-outline { background: transparent; border-color: var(--border); }
.button-outline:hover { background: var(--surface-alt); }
.button-danger { background: transparent; border-color: #fbcaca; color: var(--danger); }
.button-danger:hover { background: #fef2f2; }
.button-small { padding: 5px 10px; font-size: 12px; }
.button.full { width: 100%; }
.icon-button { border: none; background: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); }

/* Forms ------------------------------------------------------------------- */

.field-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field-group label { font-weight: 600; font-size: 13px; }
input, select, textarea {
  font: inherit; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--surface); color: var(--text); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
textarea { resize: vertical; }
.hint { color: var(--muted); font-size: 12px; }
.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.checkbox input { width: auto; }
.field-inline { flex-direction: column; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
@media (max-width: 700px) { .form-grid { grid-template-columns: 1fr; } }

.filter-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.filter-input { width: auto; min-width: 160px; flex: 0 1 auto; }
.filter-bar input[type="search"] { min-width: 260px; flex: 1; }

/* Tables ------------------------------------------------------------------ */

.table-wrapper { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-alt); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fbfcfe; }
td small { color: var(--muted); }
.empty-state { padding: 28px; text-align: center; color: var(--muted); background: var(--surface-alt); border-radius: 10px; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 14px; color: var(--muted); }

/* Badges ------------------------------------------------------------------ */

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-alt); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.badge-new, .badge-active, .badge-approved, .badge-sent, .badge-processed { background: #eff6ff; color: var(--primary); border-color: #dbeafe; }
.badge-won, .badge-completed, .badge-delivered, .badge-read { background: #ecfdf5; color: var(--success); border-color: #d1fae5; }
.badge-lost, .badge-failed, .badge-disabled, .badge-opted_out { background: #fef2f2; color: var(--danger); border-color: #fee2e2; }
.badge-pending, .badge-paused, .badge-urgent, .badge-high { background: #fffbeb; color: var(--warn); border-color: #fef3c7; }
.badge-not_connected { background: #f8fafc; color: var(--muted); }

/* Stats and charts -------------------------------------------------------- */

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat-card { background: var(--surface-alt); border: 1px solid var(--border); border-radius: 10px; padding: 14px; display: flex; flex-direction: column; gap: 2px; }
.stat-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 24px; font-weight: 700; }
.stat-hint { font-size: 12px; color: var(--warn); }

.bar-row { display: grid; grid-template-columns: 130px 1fr 40px; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 13px; }
.bar { background: var(--surface-alt); border-radius: 999px; height: 8px; overflow: hidden; }
.bar-fill { background: var(--primary); height: 100%; border-radius: 999px; min-width: 2px; }
.bar-fill.alt { background: #8b5cf6; }
.bar-row strong { text-align: right; }

/* Lists and timeline ------------------------------------------------------ */

.timeline { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; max-height: 480px; overflow-y: auto; }
.timeline li { border-left: 2px solid var(--border); padding: 0 0 6px 14px; }
.timeline-type { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--primary); }
.timeline p { margin: 2px 0; }
.timeline small { color: var(--muted); }

.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.list li { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 12px; background: var(--surface-alt); border-radius: 8px; }
.list li p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.list li.done { opacity: 0.6; }

.detail-row { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--border); }
.detail-row span { color: var(--muted); }
.note-block { background: var(--surface-alt); padding: 10px 12px; border-radius: 8px; white-space: pre-wrap; }
.raw-payload { margin-top: 14px; }
.raw-payload summary { cursor: pointer; color: var(--primary); font-weight: 600; }

/* WhatsApp ---------------------------------------------------------------- */

.thread { display: grid; gap: 8px; max-height: 400px; overflow-y: auto; padding: 4px; }
.bubble { padding: 9px 12px; border-radius: 12px; max-width: 85%; }
.bubble p { margin: 0; white-space: pre-wrap; }
.bubble small { font-size: 11px; color: var(--muted); }
.bubble.inbound { background: var(--surface-alt); justify-self: start; border-bottom-left-radius: 3px; }
.bubble.outbound { background: #e0f2fe; justify-self: end; border-bottom-right-radius: 3px; }
.composer { display: flex; gap: 8px; margin-top: 12px; }
.preview-box { background: var(--surface-alt); border: 1px dashed var(--border); border-radius: 8px; padding: 12px; margin: 10px 0; white-space: pre-wrap; }

.conversation-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; align-content: start; overflow-y: auto; }
.conversation-list li { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; padding: 10px 12px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.conversation-list li:hover { background: var(--surface-alt); }
.conversation-list li.active { background: #eef2ff; border-color: #dbeafe; }
.conversation-list p { margin: 2px 0; color: var(--muted); font-size: 13px; }
.conversation-list small { color: var(--muted); font-size: 12px; }
/* Must be able to shrink, or long names/previews push the pane out of shape. */
.conversation-main { flex: 1; min-width: 0; }
.conversation-main strong, .conversation-main p, .conversation-main small {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0;
}
.conversation-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 12px; flex-shrink: 0; }

/* Inbox: WhatsApp-style two-pane chat ------------------------------------- */

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 14px;
}
.avatar.small { width: 30px; height: 30px; font-size: 12px; }

.inbox-panel { padding: 0; overflow: hidden; }
.inbox-layout { display: grid; grid-template-columns: 340px 1fr; height: 76vh; min-height: 520px; }

/* Grid and flex items default to min-width:auto, which lets long text push the
   column wider than its track. Zeroing it down the chain is what makes the
   previews ellipsize instead of running under the chat pane. */
.inbox-list { border-right: 1px solid var(--border); display: flex; flex-direction: column; background: var(--surface); min-width: 0; }
.inbox-list-header { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px 8px; }
.inbox-list-header h3 { margin: 0; }
.chat-search { margin: 0 12px 8px; width: calc(100% - 24px); border-radius: 999px; background: var(--surface-alt); border-color: transparent; padding: 8px 14px; }
.inbox-list .conversation-list { flex: 1; padding: 0 6px 10px; overflow-x: hidden; min-width: 0; grid-template-columns: minmax(0, 1fr); }
.conversation-list li { align-items: center; gap: 12px; padding: 10px 10px; border-radius: 10px; min-width: 0; }
.conversation-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; min-width: 0; }
/* flex:1 + min-width:0 is what actually lets these ellipsize instead of
   forcing the pane wider than its column. */
.conversation-top strong { flex: 1; min-width: 0; }
.conversation-time { color: var(--muted); font-size: 11px; flex-shrink: 0; }
.conversation-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; min-width: 0; }
.conversation-bottom p { margin: 1px 0 0; flex: 1; min-width: 0; }
.pill-muted { font-size: 11px; color: var(--danger); background: #fef2f2; padding: 1px 7px; border-radius: 999px; flex-shrink: 0; }

.inbox-thread { display: flex; flex-direction: column; min-width: 0; background: #efeae2; }
.chat-header { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.chat-header-name { flex: 1; min-width: 0; }
.chat-header-name strong { display: block; }
.chat-header-name small { color: var(--muted); }
.chat-header-actions { display: flex; gap: 8px; flex-shrink: 0; }

/* Subtle chat wallpaper, the way a messaging client reads. */
.chat-scroll {
  flex: 1; overflow-y: auto; padding: 16px 22px; display: flex; flex-direction: column; gap: 4px;
  background-image: radial-gradient(rgba(0, 0, 0, 0.035) 1px, transparent 1px);
  background-size: 22px 22px;
}

.day-divider { text-align: center; margin: 12px 0 8px; }
.day-divider span { background: rgba(255, 255, 255, 0.85); color: #54656f; font-size: 11px; font-weight: 600; padding: 4px 12px; border-radius: 8px; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06); }

.bubble {
  position: relative; max-width: 72%; padding: 6px 10px 6px 10px; border-radius: 8px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.09); font-size: 14px; color: #111b21;
}
.bubble p { margin: 0; white-space: pre-wrap; word-break: break-word; }
.bubble.inbound { background: #fff; align-self: flex-start; border-top-left-radius: 2px; }
.bubble.outbound { background: #d9fdd3; align-self: flex-end; border-top-right-radius: 2px; }
.bubble-tag { display: block; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: #027eb5; margin-bottom: 2px; }
/* Sits on its own line so it can never collide with a long body or an error. */
.bubble-meta { display: flex; align-items: center; justify-content: flex-end; gap: 4px; font-size: 11px; color: #667781; margin-top: 2px; }
.bubble-meta em { font-style: normal; }
.bubble-error { display: block; margin-top: 4px; font-size: 11px; color: var(--danger); }
.ticks { font-size: 11px; letter-spacing: -1px; }
.ticks.read { color: #53bdeb; }
.ticks.failed { color: var(--danger); font-weight: 700; letter-spacing: 0; }

.chat-composer { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-top: 1px solid var(--border); }
.chat-composer input { border-radius: 999px; background: var(--surface-alt); border-color: transparent; padding: 10px 16px; }
.send-button { width: 42px; height: 42px; flex-shrink: 0; border: none; border-radius: 50%; background: var(--primary); color: #fff; font-size: 16px; cursor: pointer; }
.send-button:hover { background: var(--primary-dark); }
.chat-blocked { padding: 14px; text-align: center; background: #fff8e1; color: var(--warn); border-top: 1px solid var(--border); font-weight: 500; }

.thread-empty { margin: auto; text-align: center; color: #667781; max-width: 320px; padding: 20px; }
.thread-empty-icon { font-size: 40px; margin-bottom: 8px; }

.template-body { color: var(--muted); font-size: 13px; }
.full-width { width: 100%; }

@media (max-width: 900px) {
  .inbox-layout { grid-template-columns: 1fr; height: auto; }
  .inbox-list { border-right: none; border-bottom: 1px solid var(--border); max-height: 320px; }
  .chat-scroll { min-height: 340px; }
}

/* Inline editing and explainers ------------------------------------------- */

.inline-select { width: auto; min-width: 118px; padding: 4px 8px; font-size: 13px; border-radius: 6px; }
.quick-bar { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; background: var(--surface-alt); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin-bottom: 16px; }
.quick-bar label { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; }
.quick-hint { color: var(--muted); font-size: 12px; }

.explainer { background: #f0f6ff; border: 1px solid #dbeafe; border-radius: 10px; padding: 14px 16px; margin-bottom: 16px; font-size: 13px; }
.explainer strong { display: block; margin-bottom: 4px; }
.explainer p { margin: 0 0 6px; color: #334155; }
.explainer p:last-child { margin-bottom: 0; }
.explainer em { font-style: normal; font-weight: 700; color: var(--primary); }

/* Integrations ------------------------------------------------------------ */

.integration-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; }
.integration-card { border: 1px solid var(--border); border-radius: 10px; padding: 16px; display: flex; flex-direction: column; gap: 10px; background: var(--surface-alt); }
.integration-card.connected { background: var(--surface); border-color: #bfdbfe; }
.integration-head { display: flex; justify-content: space-between; align-items: center; }
.integration-head h3 { margin: 0; }
.integration-card p { color: var(--muted); margin: 0; font-size: 13px; }
.integration-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: auto; }
.webhook-url { display: grid; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.webhook-url label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.webhook-url code { word-break: break-all; background: none; padding: 0; }

.permission-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 8px; }
.permission-grid em { font-style: normal; font-size: 11px; }

/* Alerts, modal, toast ---------------------------------------------------- */

.alert { padding: 10px 14px; border-radius: 8px; margin: 10px 0; font-weight: 500; }
.alert.error { background: #fef2f2; color: var(--danger); border: 1px solid #fee2e2; }
.alert.warn { background: #fffbeb; color: var(--warn); border: 1px solid #fef3c7; }

body.modal-open { overflow: hidden; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45); display: grid; place-items: center; padding: 20px; z-index: 50; }
.modal { background: var(--surface); border-radius: var(--radius); width: 100%; max-width: 520px; max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(15, 23, 42, 0.3); }
.modal-wide { max-width: 860px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h3 { margin: 0; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.confirm-text { margin: 0; }

.toast { position: fixed; bottom: 24px; right: 24px; background: var(--text); color: #fff; padding: 12px 18px; border-radius: 10px; opacity: 0; transform: translateY(8px); transition: opacity 0.2s, transform 0.2s; pointer-events: none; z-index: 100; max-width: 380px; }
.toast.visible { opacity: 1; transform: translateY(0); }
.toast-error { background: var(--danger); }
