:root {
  --green: #00a884;
  --green-dark: #008069;
  --green-darker: #025c4c;
  --bg: #eae6df;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --border: #e9edef;
  --text: #111b21;
  --muted: #667781;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --unread: #25d366;
  --shadow: 0 1px 3px rgba(11,20,26,.13);
  --header-h: 60px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: #d1d7db;
  overflow: hidden;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
button { cursor: pointer; font-family: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round; }

.icon-btn {
  background: transparent; border: none; color: #54656f;
  width: 40px; height: 40px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.icon-btn:hover { background: rgba(11,20,26,.06); }
.icon-btn:active { background: rgba(11,20,26,.12); }

/* ---------- LOGIN ---------- */
.login-screen {
  height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 18px;
  background: radial-gradient(1200px 500px at 50% -10%, #00b894 0%, var(--green-dark) 38%, #0b3d34 100%);
  padding: 20px;
}
.login-box {
  width: 380px; max-width: 100%;
  background: var(--panel);
  padding: 30px 26px 26px;
  border-radius: 16px;
  box-shadow: 0 18px 50px rgba(0,0,0,.28);
  display: flex; flex-direction: column; gap: 10px;
  animation: pop .25s ease;
}
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: 0; } }
.login-logo { display: flex; justify-content: center; margin-bottom: 4px; }
.login-logo img { width: 76px; height: 76px; border-radius: 20px; box-shadow: var(--shadow); }
.login-box h1 { margin: 4px 0 0; color: var(--green-darker); font-size: 22px; text-align: center; }
.login-box > .muted { text-align: center; margin: 0 0 8px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.field input {
  padding: 12px 14px; border: 1.5px solid var(--border); border-radius: 10px; font-size: 15px;
  transition: border-color .15s; background: #fff; color: var(--text);
}
.field input:focus { outline: none; border-color: var(--green); }
#login-btn {
  margin-top: 8px; padding: 13px; background: var(--green); color: #fff; border: none;
  border-radius: 10px; font-size: 15px; font-weight: 600; transition: background .15s, transform .1s;
}
#login-btn:hover { background: var(--green-dark); }
#login-btn:active { transform: scale(.99); }
#login-btn:disabled { opacity: .6; cursor: default; }
.error { color: #d9304f; font-size: 13px; min-height: 16px; text-align: center; }
.login-foot { font-size: 12px; color: rgba(255,255,255,.7); }

/* ---------- APP LAYOUT ---------- */
.app { height: 100%; display: flex; background: var(--bg); }
.sidebar {
  width: 30%; min-width: 320px; max-width: 460px; background: var(--panel);
  display: flex; flex-direction: column; border-right: 1px solid var(--border);
}
.sidebar-header {
  height: var(--header-h); flex-shrink: 0;
  background: var(--panel-2); padding: 0 10px 0 16px;
  display: flex; align-items: center; justify-content: space-between;
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, var(--green), var(--green-dark)); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 600; font-size: 15px;
}
.me-info { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.me-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-role { font-size: 11px; }
.actions { display: flex; align-items: center; gap: 2px; }

.menu-wrap { position: relative; }
.dropdown {
  position: absolute; right: 0; top: 46px; z-index: 30;
  background: #fff; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.18);
  min-width: 200px; overflow: hidden; padding: 6px 0;
  animation: pop .12s ease;
}
.dropdown button {
  display: block; width: 100%; text-align: left; padding: 12px 18px; border: none;
  background: transparent; font-size: 14px; color: var(--text);
}
.dropdown button:hover { background: var(--panel-2); }
.dropdown button.danger { color: #d9304f; }

.wa-banner {
  padding: 8px 14px; font-size: 13px; text-align: center;
  background: #fff3cd; color: #664d03; border-bottom: 1px solid var(--border);
}
.wa-banner.ok { background: #d9f3e6; color: #0f5132; }
.wa-banner.bad { background: #fde0e3; color: #842029; cursor: pointer; font-weight: 500; }

.search-wrap { position: relative; padding: 8px 12px; }
.search-ico { position: absolute; left: 24px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; color: var(--muted); }
.search {
  width: 100%; padding: 9px 14px 9px 40px; border: none; border-radius: 10px;
  background: var(--panel-2); font-size: 14px; color: var(--text);
}
.search:focus { outline: none; box-shadow: 0 0 0 2px rgba(0,168,132,.25); }

.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  display: flex; gap: 12px; padding: 11px 14px; cursor: pointer; align-items: center;
}
.chat-item:hover { background: #f5f6f6; }
.chat-item.active { background: #e9edef; }
.chat-item .avatar { background: linear-gradient(135deg, #b9c2c9, #8c969d); }
.chat-meta { flex: 1; min-width: 0; border-bottom: 1px solid #f2f2f2; padding-bottom: 11px; margin-top: -11px; padding-top: 11px; }
.chat-item:last-child .chat-meta { border-bottom: none; }
.chat-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.chat-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread-badge {
  background: var(--unread); color: #fff; font-size: 11px; font-weight: 600;
  min-width: 20px; height: 20px; border-radius: 10px; display: inline-flex;
  align-items: center; justify-content: center; padding: 0 6px; flex-shrink: 0;
}

/* ---------- CONVERSA ---------- */
.conversation { flex: 1; display: flex; flex-direction: column; position: relative; min-width: 0; }
.empty-state {
  flex: 1; display: flex; align-items: center; justify-content: center; text-align: center;
  background: var(--panel-2); border-bottom: 6px solid var(--green); padding: 20px;
}
.empty-inner { max-width: 420px; }
.empty-logo-img { width: 96px; height: 96px; border-radius: 24px; opacity: .9; margin-bottom: 14px; }
.empty-logo { font-size: 26px; color: var(--green-darker); font-weight: 700; margin-bottom: 6px; }
.chat-view { flex: 1; display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-header {
  height: var(--header-h); flex-shrink: 0;
  background: var(--panel-2); padding: 0 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
}
.chat-header .avatar { background: linear-gradient(135deg, #b9c2c9, #8c969d); }
.chat-head-info { min-width: 0; }
.chat-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 12px; }
.back-btn { display: none; margin-left: -8px; }

.messages {
  flex: 1; overflow-y: auto; padding: 18px 7%;
  background-color: #efeae2;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Cpath d='M0 0h40v40H0z' fill='%23efeae2'/%3E%3Ccircle cx='20' cy='20' r='1' fill='%23d9d2c8'/%3E%3C/svg%3E");
  display: flex; flex-direction: column; gap: 3px;
}
.msg { max-width: 70%; padding: 6px 9px 8px; border-radius: 10px; font-size: 14.2px;
  box-shadow: var(--shadow); position: relative; word-wrap: break-word; line-height: 1.35; }
.msg.in { align-self: flex-start; background: var(--bubble-in); border-top-left-radius: 2px; }
.msg.out { align-self: flex-end; background: var(--bubble-out); border-top-right-radius: 2px; }
.msg .agent { font-size: 12px; font-weight: 700; color: var(--green-dark); margin-bottom: 2px; }
.msg .sender { font-size: 12px; font-weight: 700; color: #d9304f; margin-bottom: 2px; }
.msg .body { white-space: pre-wrap; }
.msg .meta { font-size: 10.5px; color: var(--muted); text-align: right; margin-top: 2px; display: flex; gap: 4px; justify-content: flex-end; align-items: center; }
.msg .ticks.read { color: #53bdeb; }
.msg img.media, .msg video.media { max-width: 280px; border-radius: 8px; display: block; cursor: pointer; }
.msg .doc { display: flex; align-items: center; gap: 8px; padding: 10px; background: rgba(0,0,0,.05); border-radius: 8px; text-decoration: none; color: var(--text); }
.msg audio { width: 250px; max-width: 60vw; }

.composer {
  position: relative;
  display: flex; align-items: flex-end; gap: 6px; padding: 8px 14px;
  background: var(--panel-2); border-top: 1px solid var(--border);
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}
.composer-main { display: flex; align-items: flex-end; gap: 6px; flex: 1; }
.composer .attach { cursor: pointer; }
.composer textarea {
  flex: 1; resize: none; border: none; border-radius: 22px; padding: 11px 16px;
  font-size: 15px; font-family: inherit; max-height: 120px; line-height: 1.3;
  background: var(--panel);
}
.composer textarea:focus { outline: none; }

/* ---------- EMOJI PICKER ---------- */
.emoji-panel {
  position: absolute; bottom: calc(100% + 6px); left: 10px;
  width: 352px; max-width: calc(100% - 20px); height: 300px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: 0 10px 34px rgba(11,20,26,.22);
  display: flex; flex-direction: column; overflow: hidden; z-index: 30;
  animation: pop .14s ease;
}
.emoji-search {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.emoji-search svg { width: 16px; height: 16px; color: var(--muted); flex-shrink: 0; }
.emoji-search input {
  flex: 1; border: none; outline: none; background: transparent;
  font-size: 14px; font-family: inherit; color: var(--text);
}
.emoji-empty {
  flex: 1; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 14px; padding: 20px;
}
.emoji-cats {
  display: flex; gap: 2px; padding: 6px 8px; border-bottom: 1px solid var(--border);
  overflow-x: auto; flex-shrink: 0;
}
.emoji-cats button {
  background: transparent; border: none; font-size: 19px; line-height: 1;
  padding: 5px 7px; border-radius: 8px; flex-shrink: 0; opacity: .65;
  transition: background .12s, opacity .12s;
}
.emoji-cats button:hover { background: var(--panel-2); opacity: 1; }
.emoji-cats button.active { background: var(--panel-2); opacity: 1; }
.emoji-grid {
  flex: 1; overflow-y: auto; padding: 8px;
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px;
}
.emoji-grid button {
  background: transparent; border: none; font-size: 23px; line-height: 1;
  padding: 4px; border-radius: 8px; aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
  transition: background .1s;
}
.emoji-grid button:hover { background: var(--panel-2); }
.emoji-grid button:active { transform: scale(.9); }
@media (max-width: 820px) {
  .emoji-panel { width: calc(100% - 20px); height: 38vh; }
}

/* ---------- GRAVAÇÃO DE ÁUDIO ---------- */
.rec-bar {
  display: flex; align-items: center; gap: 10px; flex: 1;
  background: var(--panel); border-radius: 24px; padding: 6px 8px 6px 6px;
  animation: pop .14s ease;
}
.rec-cancel { color: #e53935; }
.rec-dot {
  width: 11px; height: 11px; border-radius: 50%; background: #e53935;
  flex-shrink: 0; animation: recpulse 1.1s ease-in-out infinite;
}
@keyframes recpulse { 0%,100% { opacity: 1; } 50% { opacity: .25; } }
.rec-time { font-variant-numeric: tabular-nums; font-size: 15px; color: var(--text); min-width: 42px; }
.rec-label { flex: 1; font-size: 14px; }
.send-btn {
  background: var(--green); color: #fff; border: none; width: 46px; height: 46px;
  border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: background .15s, transform .1s;
}
.send-btn:hover { background: var(--green-dark); }
.send-btn:active { transform: scale(.94); }
.send-btn svg { fill: currentColor; stroke: none; }

/* ---------- MODAL ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(11,20,26,.55);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px;
  animation: pop .15s ease;
}
.modal {
  background: #fff; border-radius: 14px; width: 580px; max-width: 100%;
  max-height: 90vh; overflow: auto; padding: 24px;
}
.modal h2 { margin: 0 0 4px; }
.modal h3 { margin: 18px 0 8px; font-size: 15px; }
.modal .tabs { display: flex; gap: 6px; margin: 14px 0 18px; flex-wrap: wrap; }
.modal .tabs button {
  padding: 8px 14px; border: 1px solid var(--border); background: #fff; border-radius: 20px; font-size: 13px;
}
.modal .tabs button.active { background: var(--green); color: #fff; border-color: var(--green); }
.modal table { width: 100%; border-collapse: collapse; font-size: 13px; }
.modal th, .modal td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.modal input, .modal select {
  padding: 11px 12px; border: 1.5px solid var(--border); border-radius: 9px; font-size: 14px; width: 100%;
}
.modal input:focus, .modal select:focus { outline: none; border-color: var(--green); }
.modal .form-row { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.modal .form-row > * { flex: 1; min-width: 120px; }
.btn { padding: 11px 16px; border: none; border-radius: 9px; background: var(--green); color: #fff; font-weight: 600; font-size: 14px; }
.btn:hover { background: var(--green-dark); }
.btn.secondary { background: #e9edef; color: #111; }
.btn.secondary:hover { background: #dce1e3; }
.btn.danger { background: #d9304f; }
.btn.danger:hover { background: #c02744; }
.btn.block { width: 100%; }
.btn-x { background: transparent; border: none; font-size: 24px; line-height: 1; float: right; color: var(--muted); cursor: pointer; }
.modal.modal-sm { width: 400px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.new-number { display: flex; gap: 8px; margin: 14px 0; }
.new-number input { flex: 1; }
.new-number .btn { white-space: nowrap; }
.agenda-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.agenda-head .search { flex: 1; min-width: 140px; position: static; }
.agenda-head .btn { padding: 9px 12px; font-size: 13px; }
.contact-list { display: flex; flex-direction: column; max-height: 46vh; overflow: auto; margin: 0 -6px; }
.contact-item {
  display: flex; align-items: center; gap: 12px; padding: 10px 6px;
  border-bottom: 1px solid var(--border); border-radius: 8px;
}
.contact-item:hover { background: #f5f6f6; }
.contact-meta { flex: 1; min-width: 0; cursor: pointer; }
.contact-name { font-weight: 600; font-size: 14px; }
.contact-phone { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-actions { display: flex; gap: 4px; }
.contact-actions .icon-btn { width: 34px; height: 34px; }
#btn-contact.saved svg { color: var(--green); }
.qr-box { text-align: center; }
.qr-box img { width: 240px; height: 240px; max-width: 70vw; border: 8px solid #fff; box-shadow: var(--shadow); border-radius: 8px; }
.center { text-align: center; }
.right { text-align: right; }
.mt { margin-top: 12px; }
.pill { padding: 3px 9px; border-radius: 12px; font-size: 11px; font-weight: 600; }
.pill.on { background: #d9f3e6; color: #0f5132; }
.pill.off { background: #fde0e3; color: #842029; }
.hint { background: #fff8e1; border: 1px solid #ffe7a0; color: #6b5400; padding: 10px 12px; border-radius: 9px; font-size: 13px; }
.field-pass { position: relative; }

/* ---------- RESPONSIVO (mobile) ---------- */
@media (max-width: 820px) {
  :root { --header-h: 56px; }
  .sidebar { width: 100%; min-width: 0; max-width: none; }
  .conversation { position: absolute; inset: 0; transform: translateX(100%); transition: transform .22s ease; z-index: 10; }
  .app.show-conv .conversation { transform: translateX(0); }
  .back-btn { display: inline-flex; }
  .msg { max-width: 82%; }
  .messages { padding: 14px 10px; }
  .modal { padding: 18px; border-radius: 12px; }
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal { max-height: 94vh; width: 100%; border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
}
@media (display-mode: standalone) {
  .sidebar-header, .chat-header { padding-top: env(safe-area-inset-top); height: calc(var(--header-h) + env(safe-area-inset-top)); }
}
