:root {
  --bg: #f7f7f7;
  --surface: #ffffff;
  --border: #e6e6e6;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #8a8984;
  --accent: #5a2678;        /* roxo da clínica */
  --accent-2: #b42e88;      /* magenta da clínica */
  --accent-soft: #f4eff7;
  --accent-2-soft: #fbeff6;
  --danger: #c03a3a;
  --warn-bg: #fff6e0;
  --fb: #2a78d6;
  --ig: #eb6834;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 15px; }
button, input, select, textarea { font: inherit; }
a { color: var(--accent); }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login form { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 32px; width: 100%; max-width: 360px; display: grid; gap: 14px; }
.login h1 { margin: 0; font-size: 1.3rem; }
.login p { margin: 0; color: var(--text-2); font-size: .9rem; }
.brand-mark { font-size: 2rem; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: 100vh; }
main { min-width: 0; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 20px 12px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; }
.sidebar .logo { font-weight: 700; padding: 4px 10px 18px; line-height: 1.25; display: flex; align-items: center; gap: 10px; }
.brand-logo { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; flex: none; }
.brand-emoji { font-size: 1.6rem; line-height: 1; flex: none; }
.topbar, .backdrop { display: none; }
.sidebar .logo small { display: block; font-weight: 400; color: var(--muted); font-size: .8rem; }
.nav-btn { display: flex; align-items: center; justify-content: space-between; gap: 8px; text-align: left; background: none; border: 0; padding: 9px 12px; border-radius: 8px; cursor: pointer; color: var(--text-2); }
.nav-btn:hover { background: var(--bg); }
.nav-btn.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-btn .badge { background: var(--accent); color: #fff; border-radius: 999px; font-size: .72rem; padding: 1px 7px; font-weight: 600; }
.sidebar .spacer { flex: 1; }
main { padding: 28px 32px 60px; max-width: 1200px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h2 { margin: 0; font-size: 1.4rem; }
.page-head p { margin: 4px 0 0; color: var(--text-2); font-size: .9rem; }

/* ---------- componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; }
.card h3 { margin: 0 0 12px; font-size: 1rem; }
.card .sub { color: var(--text-2); font-size: .85rem; margin: -6px 0 12px; }
.grid { display: grid; gap: 16px; }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin-bottom: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(340px, 100%), 1fr)); }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.kpi .label { color: var(--text-2); font-size: .82rem; }
.kpi .value { font-size: 1.8rem; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .hint { color: var(--muted); font-size: .78rem; margin-top: 2px; }

.btn { border: 1px solid var(--border); background: var(--surface); padding: 8px 14px; border-radius: 8px; cursor: pointer; color: var(--text); white-space: nowrap; }
.btn:hover { border-color: #c9ccc7; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

input[type=text], input[type=password], input[type=number], input[type=search], input[type=tel], input[type=email], input[type=datetime-local], select, textarea {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; background: #fff; color: var(--text);
}
textarea { resize: vertical; min-height: 90px; line-height: 1.45; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
label.field { display: grid; gap: 5px; font-size: .88rem; color: var(--text-2); margin-bottom: 14px; }
label.field small { color: var(--muted); }
.toggle { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; font-size: .9rem; }
th { color: var(--text-2); font-weight: 500; font-size: .8rem; }
tr:last-child td { border-bottom: 0; }
td.actions { white-space: nowrap; text-align: right; }
.table-wrap { overflow-x: auto; }
.pill { display: inline-block; white-space: nowrap; padding: 2px 9px; border-radius: 999px; font-size: .75rem; background: var(--bg); border: 1px solid var(--border); color: var(--text-2); }
.pill.fb::before, .pill.ig::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: 0; }
.pill.fb::before { background: var(--fb); }
.pill.wa::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; background: #1baf7a; }
.pill.ig::before { background: var(--ig); }
.pill.off { opacity: .55; }
.pill.warn { background: var(--warn-bg); border-color: #f0dca8; }
.empty { color: var(--muted); text-align: center; padding: 30px 10px; }
.muted { color: var(--muted); }
.notice { background: var(--warn-bg); border: 1px solid #f0dca8; border-radius: 10px; padding: 12px 14px; font-size: .9rem; margin-bottom: 16px; }

/* checklist */
.checklist { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.checklist li { display: flex; gap: 10px; align-items: center; font-size: .9rem; }
.checklist .dot { width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; font-size: .75rem; flex: none; }
.checklist .ok { background: #dff2e5; color: #1d6b37; }
.checklist .no { background: #f3e3e3; color: var(--danger); }
code { background: var(--bg); padding: 2px 6px; border-radius: 5px; font-size: .85em; word-break: break-all; }

/* conversas */
.convs { display: grid; grid-template-columns: 320px 1fr; gap: 16px; min-height: 560px; }
.conv-list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.conv-list .search { padding: 10px; border-bottom: 1px solid var(--border); }
.conv-items { overflow-y: auto; max-height: 640px; }
.conv-item { padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; display: grid; gap: 3px; }
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: var(--accent-soft); }
.conv-item .top { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; font-size: .9rem; }
.conv-item .top span:last-child { font-weight: 400; color: var(--muted); font-size: .75rem; white-space: nowrap; }
.conv-item .preview { color: var(--text-2); font-size: .82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; min-height: 560px; }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.chat-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; max-height: 560px; background: #fafbfa; }
.chat-foot { padding: 10px; border-top: 1px solid var(--border); display: flex; gap: 8px; }
.chat-foot textarea { min-height: 44px; height: 44px; }
.bubble { max-width: 78%; padding: 9px 13px; border-radius: 14px; white-space: pre-wrap; font-size: .9rem; line-height: 1.4; }
.bubble.user { align-self: flex-start; background: #fff; border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble.bot { align-self: flex-end; background: var(--accent-soft); border-bottom-right-radius: 4px; }
.bubble.human { align-self: flex-end; background: #e9eefb; border-bottom-right-radius: 4px; }
.bubble .meta { display: block; font-size: .7rem; color: var(--muted); margin-top: 4px; }
.bubble.typing { color: var(--muted); font-style: italic; }
.sim-info { font-size: .8rem; color: var(--text-2); align-self: flex-end; max-width: 78%; display: flex; gap: 6px; flex-wrap: wrap; }

/* gráficos */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.legend { display: flex; gap: 16px; font-size: .82rem; color: var(--text-2); margin-bottom: 6px; flex-wrap: wrap; }
.legend span::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; background: var(--c); vertical-align: -1px; }
.tooltip { position: absolute; pointer-events: none; background: #1d1d1b; color: #fff; padding: 7px 10px; border-radius: 8px; font-size: .78rem; line-height: 1.45; max-width: 240px; transform: translate(-50%, calc(-100% - 10px)); white-space: normal; z-index: 5; display: none; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal { background: var(--surface); border-radius: 14px; padding: 22px; width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto; }
.modal h3 { margin: 0 0 16px; }
.modal .row { justify-content: flex-end; margin-top: 6px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1d1d1b; color: #fff; padding: 10px 18px; border-radius: 10px; font-size: .9rem; z-index: 30; }
.toast.err { background: var(--danger); }

/* ---------- telemóvel ---------- */
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .grid.kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi { padding: 12px 14px; }
  .kpi .value { font-size: 1.5rem; }
  .page-head .row { width: 100%; }
  .page-head .row select { flex: 1; min-width: 0; }
  .card { padding: 14px; }
  /* tabelas viram cartões */
  .table-wrap table, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; width: 100%; }
  .table-wrap tr:first-child:has(th) { display: none; }
  .table-wrap tr { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; background: var(--surface); }
  .table-wrap td { border: 0; padding: 4px 0; }
  .table-wrap td[data-label]::before { content: attr(data-label); display: block; font-size: .72rem; color: var(--muted); margin-bottom: 1px; }
  .table-wrap td.actions { text-align: left; white-space: normal; padding-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }
  .table-wrap td.actions .btn { flex: 1; }
  .table-wrap select { width: 100%; }
  .chat-head .row { flex-wrap: nowrap; }
  .modal { padding: 18px; }
  /* barra de topo com logótipo + menu lateral deslizante */
  .topbar { display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 15; background: var(--surface); border-bottom: 1px solid var(--border); padding: 10px 14px; }
  .topbar-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .topbar-brand b { display: block; font-size: .98rem; }
  .topbar-brand small { display: block; color: var(--muted); font-size: .78rem; }
  .menu-btn { position: relative; background: none; border: 1px solid var(--border); border-radius: 10px; width: 42px; height: 42px; font-size: 1.3rem; cursor: pointer; color: var(--text); flex: none; }
  .menu-dot { position: absolute; top: 6px; right: 6px; width: 9px; height: 9px; border-radius: 50%; background: var(--danger); }
  .sidebar { position: fixed; top: 0; left: 0; bottom: 0; z-index: 30; width: 270px; max-width: 82vw; height: 100vh; overflow-y: auto; transform: translateX(-105%); transition: transform .22s ease; box-shadow: 4px 0 24px rgba(0,0,0,.12); }
  body.menu-open .sidebar { transform: none; }
  .backdrop { display: block; position: fixed; inset: 0; z-index: 25; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .22s; }
  body.menu-open .backdrop { opacity: 1; pointer-events: auto; }
  body.menu-open { overflow: hidden; }
  .nav-btn { padding: 12px; font-size: 1rem; }
  main { padding: 18px 14px 60px; }
  .convs { grid-template-columns: 1fr; }
  .convs.has-open .conv-list { display: none; }
  .convs:not(.has-open) .chat { display: none; }
  .bubble { max-width: 90%; }
}
@media (min-width: 861px) { .only-mobile { display: none !important; } }

/* ---------- identidade da clínica, ícones e novidades ---------- */
.icon { width: 18px; height: 18px; flex: none; vertical-align: -3px; }
.icon .i-fill { fill: var(--surface); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn .icon { width: 16px; height: 16px; }
.btn.small .icon { width: 14px; height: 14px; }
.btn.icon-only { padding: 6px 8px; }
.btn.primary .icon .i-fill { fill: var(--accent); }
.row.end { justify-content: flex-end; }
.row.tight { gap: 4px; }
.pill { display: inline-flex; align-items: center; gap: 4px; }
.pill .icon { width: 13px; height: 13px; }
.pill.fb, .pill.ig, .pill.wa { display: inline-block; }
.pill.new { background: var(--accent-2-soft); border-color: #f1cfe3; color: var(--accent-2); font-weight: 600; }
.pill.count { background: var(--accent-2); border-color: var(--accent-2); color: #fff; font-weight: 600; }
.pill.lead { background: var(--accent-soft); border-color: #dccbe8; color: var(--accent); }
.pill.status-on i { width: 8px; height: 8px; border-radius: 50%; background: #2e9d5b; display: inline-block; }
.pill.role-admin { background: var(--accent-soft); color: var(--accent); border-color: #dccbe8; }
.pill.role-dono { background: var(--accent-2-soft); color: var(--accent-2); border-color: #f1cfe3; }
.nav-btn { justify-content: flex-start; gap: 10px; }
.nav-btn .nav-label { flex: 1; }
.nav-btn .icon { color: var(--muted); }
.nav-btn.active .icon { color: var(--accent); }
.nav-btn.active { box-shadow: inset 3px 0 0 var(--accent-2); }
.nav-btn .badge { background: var(--accent-2); }
.sidebar .logo { flex-direction: row; align-items: center; gap: 10px; padding: 4px 10px 18px; }
.sidebar .logo small { display: block; font-weight: 400; color: var(--muted); font-size: .8rem; margin: 0; }
.brand-logo { width: 44px; height: 36px; object-fit: contain; border-radius: 0; flex: none; }
.me { display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin: 6px 0 2px; border-top: 1px solid var(--border); color: var(--text-2); }
.me b { display: block; font-size: .9rem; color: var(--text); }
.me small { color: var(--muted); font-size: .78rem; }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.login-brand b { display: block; font-size: 1.05rem; }
.login-brand small { display: block; color: var(--muted); font-size: .82rem; }
.login h1 { color: var(--accent); }
.dot .icon { width: 13px; height: 13px; }
.checklist .dot { width: 22px; height: 22px; }
.conv-item.unread .who { font-weight: 700; color: var(--text); }
.conv-item .who { display: inline-flex; align-items: center; gap: 6px; min-width: 0; }
.unread-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); flex: none; display: inline-block; }
.muted-ic { width: 14px; height: 14px; color: var(--muted); }
.bubble.bot { background: var(--accent-soft); }
.bubble.human { background: var(--accent-2-soft); }
a.phone { display: inline-flex; align-items: center; gap: 5px; font-weight: 600; color: var(--accent); text-decoration: none; }
a.phone .icon { width: 15px; height: 15px; }
.page-head h2 { color: var(--text); }
.kpi .value { color: var(--text); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.topbar-brand b { display: block; font-size: .98rem; }
.topbar-brand small { display: block; color: var(--muted); font-size: .78rem; }
.menu-btn { display: inline-flex; align-items: center; justify-content: center; }
.menu-btn .icon { width: 22px; height: 22px; }
.menu-dot { background: var(--accent-2) !important; }
@media (max-width: 860px) { .hide-sm { display: none; } }

.modal .notice { margin-bottom: 14px; }

td select[data-st] { min-width: 130px; }
@media (min-width: 861px) {
  td.actions { white-space: normal; text-align: right; }
  td.actions .btn { margin: 2px 0 2px 4px; }
}

/* Backup / relatórios */
.reports { padding: 6px 20px; }
.report-row { display: flex; align-items: center; gap: 14px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.report-row:last-child { border-bottom: 0; }
.report-icon { flex: none; width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; background: #f6eef4; color: #B42E88; }
.report-icon .icon { width: 20px; height: 20px; }
.report-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.report-info b { font-size: .95rem; }
.report-info span { color: #5A2678; font-weight: 500; }
.report-info small { color: var(--text-2); font-size: .8rem; }
.report-actions { display: flex; gap: 6px; flex: none; }
.report-actions a.btn { text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
@media (max-width: 640px) { .report-row { flex-wrap: wrap; } .report-actions { width: 100%; justify-content: flex-end; } }
[hidden] { display: none !important; }

.pill.ins { background: #f6eef4; color: #B42E88; }
.pill.plain { background: #eef0f3; color: #555; }
.icon.flip { transform: rotate(180deg); }
.modal .table-wrap table { font-size: .85rem; }
a.btn { text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
