:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface2: #243044;
  --border: #2d3f56;
  --text: #e8edf4;
  --muted: #8b9cb3;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --wa: #25d366;
  --wa-hover: #1ebe57;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --radius: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,.35);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

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

.container { max-width: 1100px; margin: 0 auto; padding: 0 1rem; }

/* Header */
.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .75rem 1rem;
  flex-wrap: wrap;
}

.logo {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  flex-shrink: 0;
}

.logo:hover { text-decoration: none; }

.logo-img {
  height: 32px;
  width: auto;
  display: block;
}

.nav { display: flex; gap: .5rem; flex: 1; align-items: center; }
.nav a { color: var(--muted); padding: .4rem .75rem; border-radius: 6px; text-decoration: none; }
.nav a:hover { background: var(--surface2); color: var(--text); text-decoration: none; }

.nav-badge {
  display: inline-block;
  min-width: 1.1rem;
  padding: .1rem .35rem;
  font-size: .65rem;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  background: #ca8a04;
  color: #1c1917;
  border-radius: 999px;
  vertical-align: middle;
}

.user-menu { display: flex; align-items: center; gap: .75rem; color: var(--muted); font-size: .9rem; }

.main { padding: 1.5rem 1rem 3rem; }

.footer {
  text-align: center;
  padding: 1.5rem;
  color: var(--muted);
  font-size: .85rem;
  border-top: 1px solid var(--border);
}

/* Typography */
.page-header { margin-bottom: 1.5rem; }
.page-header h1 { font-size: 1.75rem; font-weight: 700; }
.subtitle { color: var(--muted); margin-top: .25rem; }
.row-between { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; }
.header-actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .35rem;
  padding: .55rem 1rem;
  border-radius: 8px;
  border: none;
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background .15s;
  white-space: nowrap;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); text-decoration: none; color: #fff; }

.btn-wa { background: var(--wa); color: #fff; }
.btn-wa:hover { background: var(--wa-hover); text-decoration: none; color: #fff; }

.btn-ghost { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--border); text-decoration: none; color: var(--text); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: .35rem .65rem; font-size: .8rem; }
.btn-block { width: 100%; }

.inline-form { display: inline; }

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: .75rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  text-align: center;
}

.stat-value { display: block; font-size: 1.75rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: .8rem; }
.stat-warning .stat-value { color: var(--warning); }
.stat-danger .stat-value { color: var(--danger); }
.stat-success .stat-value { color: var(--success); }
.stat-info .stat-value { color: var(--primary); }

/* Sections & cards */
.section { margin-bottom: 2rem; }
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.section-header h2 { font-size: 1.1rem; }

.card, .card-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.card-list { display: flex; flex-direction: column; gap: .5rem; }
.card-item { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

/* Tables */
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { padding: .75rem 1rem; text-align: left; border-bottom: 1px solid var(--border); }
.table th { background: var(--surface2); color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: rgba(255,255,255,.02); }
.table small { display: block; color: var(--muted); font-size: .8rem; }
.table .empty { text-align: center; color: var(--muted); padding: 2rem; }
.actions { white-space: nowrap; }

.client-name { font-weight: 600; color: var(--text); }

/* Mobile cards - hidden on desktop */
.mobile-cards { display: none; }

/* Badges */
.badge {
  display: inline-block;
  padding: .2rem .55rem;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.badge-green { background: #14532d; color: #86efac; }
.badge-yellow { background: #422006; color: #fde047; }
.badge-blue { background: #1e3a5f; color: #93c5fd; }
.badge-red { background: #450a0a; color: #fca5a5; }
.badge-orange { background: #451a03; color: #fdba74; }
.badge-gray { background: #374151; color: #d1d5db; }
.badge-slate { background: #1e293b; color: #94a3b8; }

.text-red { color: #fca5a5; }
.muted { color: var(--muted); font-size: .875rem; }

/* Filters */
.filters {
  display: flex;
  gap: .5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.filters input, .filters select {
  flex: 1;
  min-width: 160px;
}

/* Forms */
.form label { display: block; margin-bottom: 1rem; font-size: .875rem; font-weight: 500; }
.form input, .form select, .form textarea, .filters input, .filters select {
  display: block;
  width: 100%;
  margin-top: .35rem;
  padding: .6rem .75rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: .95rem;
}

.form input:focus, .form select:focus, .form textarea:focus, .filters input:focus {
  outline: none;
  border-color: var(--primary);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
}

.fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem 1.25rem;
  background: var(--surface);
}

.fieldset legend { padding: 0 .5rem; font-weight: 600; font-size: .9rem; color: var(--muted); }
.full-width { grid-column: 1 / -1; }

.form-actions { display: flex; gap: .75rem; flex-wrap: wrap; padding-top: .5rem; }

.inline-form-row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: flex-end; }
.inline-form-row select { width: auto; min-width: 120px; }
.inline-form-row input { flex: 1; min-width: 200px; }

/* Detail */
.detail-list { display: grid; grid-template-columns: auto 1fr; gap: .35rem 1.25rem; font-size: .9rem; }
.detail-list dt { color: var(--muted); }
.notes-box { margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--border); }
.notes-box p { margin-top: .35rem; color: var(--muted); white-space: pre-wrap; }
.hint { margin-top: .75rem; font-size: .8rem; color: var(--muted); }

/* Timeline */
.timeline { list-style: none; }
.timeline li {
  padding: .65rem 0;
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: 80px 1fr auto;
  gap: .5rem;
  align-items: start;
  font-size: .875rem;
}
.timeline li:last-child { border-bottom: none; }
.timeline-type { color: var(--primary); font-weight: 600; text-transform: capitalize; font-size: .8rem; }
.timeline small { color: var(--muted); white-space: nowrap; }
.mt { margin-top: 1.25rem; }

.danger-zone { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border); }

/* Alerts */
.alert {
  padding: .75rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: .9rem;
}
.alert-error { background: #450a0a; color: #fca5a5; border: 1px solid #7f1d1d; }
.alert-success { background: #14532d; color: #86efac; border: 1px solid #166534; }

.empty-state { text-align: center; padding: 3rem 1rem; color: var(--muted); }
.empty-state p { margin-bottom: 1rem; font-size: 1.1rem; }

/* Login */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow);
}

.login-header { text-align: center; margin-bottom: 1.5rem; }
.login-logo {
  height: 48px;
  width: auto;
  max-width: 220px;
  margin: 0 auto 1rem;
  display: block;
}
.login-header p { color: var(--muted); font-size: .9rem; }
.login-hint { margin-top: 1.25rem; text-align: center; font-size: .8rem; color: var(--muted); }
.login-hint code { background: var(--surface2); padding: .15rem .4rem; border-radius: 4px; }

.error-page { text-align: center; padding: 4rem 1rem; }
.error-page h1 { font-size: 3rem; margin-bottom: 1rem; }
.error-page p { color: var(--muted); margin-bottom: 1.5rem; }

.row-inactive { opacity: 0.65; }

.checkbox-label {
  display: flex !important;
  align-items: center;
  gap: .5rem;
  flex-direction: row !important;
  margin-top: .5rem;
}

.checkbox-label input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.plan-custom-fields {
  display: contents;
}

/* WhatsApp */
.wa-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}

.wa-status-row {
  display: flex;
  align-items: center;
  gap: .65rem;
  font-size: 1.1rem;
  font-weight: 600;
  margin: 1rem 0;
}

.wa-status-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--muted);
}

.wa-status-connected { background: var(--success); box-shadow: 0 0 8px var(--success); }
.wa-status-qr { background: var(--warning); animation: pulse 1.5s infinite; }
.wa-status-connecting { background: var(--primary); animation: pulse 1s infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .4; }
}

.wa-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin: 1rem 0; }
.wa-inline-error { margin-top: .75rem; }
.wa-qr-wrap { display: flex; justify-content: center; padding: 1rem; background: #fff; border-radius: 8px; margin-top: 1rem; }
.wa-qr-wrap img { max-width: 280px; height: auto; }
.wa-steps { margin: .75rem 0 0 1.25rem; color: var(--muted); font-size: .9rem; }
.wa-steps li { margin-bottom: .35rem; }
.wa-notice ul { margin: .75rem 0 0 1.25rem; color: var(--muted); font-size: .9rem; }
.wa-notice li { margin-bottom: .4rem; }

/* Chat */
.main-full {
  max-width: none;
  padding: 0;
  margin: 0;
  width: 100%;
}

.main-full + .footer { display: none; }

.chat-app {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: calc(100vh - 56px);
  min-height: 480px;
  background: var(--bg);
}

.chat-sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}

.chat-sidebar-header h1 { font-size: 1.25rem; margin: 0; }

.wa-status-pill {
  font-size: .7rem;
  padding: .2rem .5rem;
  border-radius: 999px;
  font-weight: 600;
}

.wa-status-pill.wa-status-connected { background: #14532d; color: #86efac; }
.wa-status-pill.wa-status-disconnected,
.wa-status-pill.wa-status-qr,
.wa-status-pill.wa-status-connecting { background: #374151; color: #d1d5db; }

.chat-sidebar-alert {
  padding: .75rem 1rem;
  background: #451a03;
  font-size: .85rem;
}

.chat-search-wrap { padding: .75rem 1rem; }
.chat-search {
  width: 100%;
  padding: .55rem .75rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}

.chat-conversations {
  flex: 1;
  overflow-y: auto;
}

.chat-empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--muted);
  font-size: .9rem;
}

.chat-conv-item {
  display: block;
  padding: .85rem 1rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  position: relative;
}

.chat-conv-item:hover,
.chat-conv-item.active {
  background: var(--surface2);
  text-decoration: none;
}

.chat-conv-top {
  display: flex;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: .25rem;
}

.chat-conv-top small { color: var(--muted); white-space: nowrap; }
.chat-conv-preview { font-size: .85rem; color: var(--muted); padding-right: .5rem; margin-bottom: .35rem; }
.chat-conv-meta { display: flex; flex-wrap: wrap; gap: .35rem; }
.chat-conv-unassigned { border-left: 3px solid #ca8a04; }
.chat-conv-badge { position: absolute; right: 1rem; bottom: .85rem; font-size: .65rem; }

.chat-sidebar-alert-warn { background: #422006; color: #fde047; }

.chat-take-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin: .75rem 1.25rem 0;
  padding: .75rem 1rem;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 8px;
  font-size: .9rem;
}

.chat-take-banner p { margin: 0; }

.chat-notifications {
  display: flex;
  flex-direction: column;
  gap: .5rem;
  padding: .75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.chat-notif {
  display: flex;
  align-items: flex-start;
  gap: .5rem;
  padding: .6rem .75rem;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 8px;
  font-size: .85rem;
}

.chat-notif p { margin: 0; flex: 1; }

.chat-notif-dismiss {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

.chat-notif-dismiss:hover { color: var(--text); }

.chat-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.chat-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  gap: .5rem;
}

.chat-placeholder-icon { font-size: 3rem; opacity: .5; }

.chat-thread-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-thread-header h2 { margin: 0; font-size: 1.1rem; }

.chat-alert { margin: .75rem 1.25rem 0; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.chat-bubble {
  max-width: 75%;
  padding: .55rem .85rem;
  border-radius: 12px;
  font-size: .95rem;
}

.chat-bubble p { margin: 0; white-space: pre-wrap; word-break: break-word; }
.chat-bubble time {
  display: block;
  font-size: .7rem;
  margin-top: .25rem;
  opacity: .7;
  text-align: right;
}

.chat-bubble-in {
  align-self: flex-start;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.chat-bubble-out {
  align-self: flex-end;
  background: #0d4d2a;
  border: 1px solid #14532d;
  border-bottom-right-radius: 4px;
}

.chat-compose {
  display: flex;
  gap: .5rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
  align-items: flex-end;
}

.chat-attach-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 1.2rem;
  flex-shrink: 0;
}

.chat-attach-btn:hover { background: var(--border); }
.chat-attach-btn input { display: none; }

.chat-compose-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: .35rem;
  min-width: 0;
}

.chat-file-preview {
  font-size: .8rem;
  color: var(--muted);
  padding: 0 .15rem;
}

.chat-compose textarea {
  width: 100%;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  padding: .65rem .85rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: inherit;
  font-size: .95rem;
}

.chat-compose textarea:focus { outline: none; border-color: var(--primary); }

.chat-media { margin-bottom: .35rem; }

.chat-media-img {
  display: block;
  max-width: 100%;
  max-height: 280px;
  border-radius: 8px;
  object-fit: cover;
}

.chat-media-video {
  display: block;
  max-width: 100%;
  max-height: 280px;
  border-radius: 8px;
}

.chat-media-audio {
  width: 100%;
  min-width: 220px;
  max-width: 320px;
}

.chat-media-doc {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .6rem .75rem;
  background: rgba(0,0,0,.2);
  border-radius: 8px;
  color: inherit;
  text-decoration: none;
  font-size: .9rem;
}

.chat-media-doc:hover { text-decoration: none; opacity: .9; }
.chat-media-doc-icon { font-size: 1.25rem; }

.chat-bubble .chat-media + p { margin-top: .35rem; }

/* Responsive */
@media (max-width: 768px) {
  .table-wrap { display: none; }
  .mobile-cards { display: flex; flex-direction: column; gap: .5rem; }

  .mobile-card {
    display: block;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    color: var(--text);
    text-decoration: none;
  }
  .mobile-card:hover { border-color: var(--primary); text-decoration: none; }
  .mobile-card-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: .35rem; }

  .header-inner { flex-direction: column; align-items: stretch; }
  .nav { justify-content: center; }
  .user-menu { justify-content: center; }

  .timeline li { grid-template-columns: 1fr; gap: .15rem; }
  .row-between { flex-direction: column; }

  .chat-app {
    grid-template-columns: 1fr;
    height: auto;
    min-height: calc(100vh - 56px);
  }

  .chat-sidebar { max-height: 40vh; border-right: none; border-bottom: 1px solid var(--border); }
  .chat-main { min-height: 50vh; }
}
