:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --sidebar: #0f172a;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #1e293b;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- Auth layout --- */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem 1rem;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 55%, #2563eb 100%);
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border-radius: calc(var(--radius) + 4px);
  padding: 2rem;
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.18);
}
.auth-brand {
  font-weight: 700;
  font-size: 1.125rem;
  letter-spacing: -0.02em;
  margin-bottom: 0.25rem;
}
.auth-sub { color: var(--muted); font-size: 0.875rem; margin-bottom: 1.5rem; }

/* --- App shell --- */
.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px 1fr;
}
.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-text);
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.sidebar .brand {
  color: #fff;
  font-weight: 700;
  font-size: 1.05rem;
  padding: 0.5rem 0.75rem;
}
.sidebar .workspace {
  font-size: 0.75rem;
  color: #94a3b8;
  padding: 0 0.75rem 0.75rem;
  border-bottom: 1px solid #1e293b;
}
.nav a {
  display: block;
  color: var(--sidebar-text);
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  text-decoration: none;
  font-size: 0.9rem;
}
.nav a:hover { background: var(--sidebar-active); color: #fff; text-decoration: none; }
.nav a.active { background: var(--primary); color: #fff; }

.main {
  padding: 1.5rem 2rem 2.5rem;
  max-width: 1200px;
}
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
}
.page-header h1 {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: -0.02em;
}
.user-pill {
  font-size: 0.8rem;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}
.grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}
.stat .label { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat .value { font-size: 1.75rem; font-weight: 700; margin-top: 0.25rem; }

/* --- Forms --- */
label { display: block; font-size: 0.8125rem; font-weight: 500; margin: 0.85rem 0 0.35rem; color: #334155; }
input, textarea, select {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: #fff;
}
input:focus, textarea:focus {
  outline: 2px solid rgba(37, 99, 235, 0.25);
  border-color: var(--primary);
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  margin-top: 1rem;
  padding: 0.55rem 1rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  font-weight: 500;
  text-decoration: none;
}
.btn:hover { background: var(--primary-hover); text-decoration: none; color: #fff; }
.btn-secondary { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #f8fafc; color: var(--text); }
.error { color: var(--danger); font-size: 0.875rem; margin-bottom: 0.75rem; }
.muted { color: var(--muted); font-size: 0.875rem; }

/* --- Inbox --- */
.inbox-layout {
  display: grid;
  grid-template-columns: minmax(240px, 280px) 1fr minmax(220px, 260px);
  gap: 1rem;
  min-height: 520px;
  align-items: start;
}
.crm-panel { font-size: 0.875rem; }
.crm-panel h3 { margin: 0 0 0.75rem; font-size: 0.95rem; }
.crm-panel .badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  background: #e0e7ff;
  color: #3730a3;
  text-transform: uppercase;
}
.notes-list { margin: 0.5rem 0 0; padding: 0; list-style: none; max-height: 140px; overflow-y: auto; }
.notes-list li { padding: 0.35rem 0; border-bottom: 1px solid var(--border); font-size: 0.8rem; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.data-table th, .data-table td { text-align: left; padding: 0.5rem 0.35rem; border-bottom: 1px solid var(--border); }
.data-table th { font-weight: 600; color: var(--muted); font-size: 0.75rem; text-transform: uppercase; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-item { border-bottom: 1px solid var(--border); }
.conv-item a {
  display: block;
  padding: 0.85rem 1rem;
  color: inherit;
  text-decoration: none;
}
.conv-item a:hover { background: #f8fafc; text-decoration: none; }
.conv-item.active a { background: #eff6ff; border-left: 3px solid var(--primary); }
.conv-row-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.2rem;
}
.conv-time { font-size: 0.72rem; }
.conv-title { font-weight: 600; font-size: 0.9rem; line-height: 1.3; }
.conv-meta { font-size: 0.72rem; margin-top: 0.1rem; }
.conv-preview { color: var(--muted); font-size: 0.8rem; margin-top: 0.15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.channel-badge { font-size: 0.62rem !important; text-transform: uppercase; padding: 0.12rem 0.4rem !important; }
.channel-whatsapp { background: #dcfce7; color: #166534; }
.channel-livechat { background: #dbeafe; color: #1e40af; }
.channel-messenger { background: #e0e7ff; color: #3730a3; }
.channel-instagram { background: #fce7f3; color: #9d174d; }
.status-dot { text-transform: capitalize; }
.status-closed { opacity: 0.7; }

.thread {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 480px;
}
.thread-header { padding-bottom: 0.75rem; border-bottom: 1px solid var(--border); margin-bottom: 0.75rem; }
.thread-title { margin: 0; font-size: 1.05rem; font-weight: 600; }
.thread-sub { margin: 0.35rem 0 0; font-size: 0.8rem; display: flex; flex-wrap: wrap; align-items: center; gap: 0.35rem; }
.thread-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: #f8fafc;
  border-radius: 8px;
  margin-bottom: 1rem;
  min-height: 240px;
  max-height: min(52vh, 520px);
}
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: #f8fafc;
  border-radius: 8px;
  margin-bottom: 1rem;
}
.bubble-sender {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}
.bubble-avatar { width: 22px; height: 22px; border-radius: 50%; object-fit: cover; }
.bubble-img { max-width: 100%; max-height: 200px; border-radius: 8px; display: block; margin-bottom: 0.35rem; }
.bubble-meta { font-size: 0.7rem; margin-top: 0.35rem; }
.bubble {
  max-width: 75%;
  padding: 0.55rem 0.85rem;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.45;
}
.bubble.inbound {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--border);
}
.bubble.outbound {
  align-self: flex-end;
  background: #dbeafe;
  color: #1e3a8a;
}
.reply-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: flex-end;
}
.reply-form {
  display: flex;
  flex: 1;
  gap: 0.5rem;
  min-width: 200px;
  flex-wrap: wrap;
  align-items: center;
}
.reply-input { margin: 0; flex: 1; min-width: 160px; }
.wa-template-select { flex: 1; min-width: 180px; margin: 0; }
.wa-template-extra { width: 100%; margin: 0.25rem 0 0; font-size: 0.85rem; }
.wa-template-extra summary { cursor: pointer; }
.wa-template-only { width: 100%; }
.wa-window-hint.wa-closed { color: #b45309; }
.wa-window-hint.wa-open { color: var(--success); }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
.reply-bar input { margin: 0; flex: 1; }
.reply-bar .btn { margin: 0; white-space: nowrap; }

@media (max-width: 1100px) {
  .inbox-layout { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; }
}
