@font-face {
  font-family: "Plus Jakarta Sans";
  src: url("/static/fonts/plus-jakarta-sans-latin.woff2") format("woff2");
  font-weight: 200 800;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329,
    U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Plus Jakarta Sans";
  src: url("/static/fonts/plus-jakarta-sans-latin-ext.woff2") format("woff2");
  font-weight: 200 800;
  font-display: swap;
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329,
    U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

:root {
  --red-50: #FEF2F2; --red-100: #FEE2E2; --red-200: #FECACA; --red-300: #FCA5A5; --red-400: #F87171;
  --red-500: #EF4444; --red-600: #DC2626; --red-700: #B91C1C; --red-800: #991B1B; --red-900: #7F1D1D;

  --bg: #FCF8F8;
  --panel: #FFFFFF;
  --panel-2: #F7F1F1;
  --ink: #170609;
  --muted: #6B5B5E;
  --line: #EDE3E4;
  --accent: var(--red-600);
  --accent-ink: #FFFFFF;
  --accent-soft: var(--red-50);
  --accent-text: var(--red-700);
  --bubble-in: #FFFFFF;
  --bubble-out: var(--red-50);
  --bubble-out-line: var(--red-100);
  --chat-bg: #F6EFEF;
  --tick: #8A7C7E;
  --tick-read: var(--red-600);
  --ok: #15803D;
  --warn: #B45309;
  --shadow: 0 10px 30px rgba(23, 6, 9, .12);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0B0B0B;
    --panel: #141414;
    --panel-2: #1C1C1C;
    --ink: #EDEDED;
    --muted: #9A9A9A;
    --line: #2A2A2A;
    --accent: var(--red-600);
    --accent-soft: #1F1F1F;
    --accent-text: var(--red-400);
    --bubble-in: #1C1C1C;
    --bubble-out: #262626;
    --bubble-out-line: #303030;
    --chat-bg: #0F0F0F;
    --tick: #8A8A8A;
    --tick-read: var(--red-400);
    --ok: #4ADE80;
    --warn: #FBBF24;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Plus Jakarta Sans", system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-text); }

.app {
  height: 100vh;
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr) auto;
  grid-template-rows: 56px 1fr;
  grid-template-areas: "top top top" "side chat lead";
}
.app > * { min-width: 0; }

/* ---- topo ---------------------------------------------------------------------------------- */
.topbar {
  grid-area: top;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand-mark, .empty-mark {
  width: 30px; height: 30px; border-radius: 8px; display: block; object-fit: cover;
}
.brand-sub { color: var(--muted); font-weight: 500; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-right: 6px; }
.conn.on { background: var(--ok); }
.conn.off { background: var(--accent); }

.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
  color: var(--ink);
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--red-700); }
.btn.danger { color: var(--accent-text); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  width: 36px; height: 36px; flex: none;
  border: 0; background: transparent; border-radius: 8px; cursor: pointer;
  font-size: 18px; line-height: 1; color: var(--muted);
}
.icon-btn:hover { background: var(--panel-2); color: var(--ink); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- lista --------------------------------------------------------------------------------- */
.sidebar {
  grid-area: side;
  display: flex; flex-direction: column; min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.search { padding: 12px 12px 6px; }
.search input, .field input, .field select, .field textarea, .modal input, .modal textarea, .modal select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 10px;
}
.filters { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 12px 10px; border-bottom: 1px solid var(--line); }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 999px;
  padding: 4px 10px; font-size: 12px; font-weight: 600; cursor: pointer; color: var(--muted);
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip .n { opacity: .75; margin-left: 4px; }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv {
  display: grid; grid-template-columns: 42px 1fr auto; gap: 10px; align-items: center;
  padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line);
}
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--accent-soft); }
.conv-main { min-width: 0; }
.conv-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-prev { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.conv-time { font-size: 11px; color: var(--muted); }
.conv.unread .conv-time { color: var(--accent-text); font-weight: 700; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center;
}
.pill { font-size: 10px; font-weight: 700; padding: 2px 6px; border-radius: 999px; background: var(--panel-2); color: var(--muted); }
.pill.st-novo { background: var(--accent-soft); color: var(--accent-text); }
.pill.st-fechado { color: var(--ok); }
.pill.st-perdido { text-decoration: line-through; }
.list-empty { padding: 24px 16px; color: var(--muted); text-align: center; }

.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700; color: #fff; background: var(--red-700);
}
.avatar.c1 { background: var(--red-600); } .avatar.c2 { background: var(--red-800); }
.avatar.c3 { background: #44403C; } .avatar.c4 { background: var(--red-900); }
.avatar.c5 { background: #57534E; } .avatar.c0 { background: var(--red-500); }

/* ---- conversa ------------------------------------------------------------------------------ */
.chat { grid-area: chat; min-width: 0; min-height: 0; display: flex; background: var(--chat-bg); }
.empty { margin: auto; text-align: center; color: var(--muted); }
.empty-mark { margin: 0 auto 12px; width: 56px; height: 56px; border-radius: 14px; }
.chat-inner { display: flex; flex-direction: column; flex: 1; min-width: 0; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-title { flex: 1; min-width: 0; }
.chat-name { font-weight: 700; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { color: var(--muted); font-size: 12px; }
.window-badge { font-size: 12px; font-weight: 600; padding: 5px 10px; border-radius: 999px; background: var(--panel-2); text-align: right; }
.window-badge.open { color: var(--ok); }
.window-badge.closing { color: var(--warn); }
.window-badge.closed { color: var(--accent-text); background: var(--accent-soft); }
.window-badge small { display: block; font-weight: 500; color: var(--muted); }

.ad-banner {
  padding: 8px 16px; font-size: 13px; background: var(--accent-soft); color: var(--accent-text);
  border-bottom: 1px solid var(--line); font-weight: 600;
}
.ad-banner a { margin-left: 6px; font-weight: 500; }

.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; margin: 10px 0 6px; font-size: 11px; font-weight: 700; color: var(--muted);
       background: var(--panel); padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line); }
.more { align-self: center; margin-bottom: 8px; }
.msg { max-width: min(72%, 560px); display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; }
.bubble {
  padding: 7px 10px 5px; border-radius: 10px; background: var(--bubble-in);
  border: 1px solid var(--line); overflow-wrap: anywhere;
}
.msg.out .bubble { background: var(--bubble-out); border-color: var(--bubble-out-line); }
.msg.in .bubble { border-top-left-radius: 3px; }
.msg.out .bubble { border-top-right-radius: 3px; }
.text { white-space: pre-wrap; line-height: 1.4; }
.meta { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.tick { font-weight: 700; color: var(--tick); letter-spacing: -3px; padding-right: 3px; }
.tick.read { color: var(--tick-read); }
.tick.pending { letter-spacing: 0; }
.tick.failed { color: var(--accent-text); letter-spacing: 0; }
.err { font-size: 12px; color: var(--accent-text); margin-top: 3px; }
.tag-ad { font-size: 11px; font-weight: 700; color: var(--accent-text); margin-bottom: 3px; }
.tag-welcome { font-size: 11px; color: var(--muted); margin-bottom: 3px; text-align: right; }
.tag-template { font-size: 11px; font-weight: 700; color: var(--muted); margin-bottom: 2px; }
.media-img { display: block; max-width: 100%; max-height: 320px; border-radius: 6px; margin-bottom: 4px; cursor: zoom-in; }
.media-audio { width: 260px; max-width: 100%; display: block; margin-bottom: 2px; }
.media-video { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; }
.doc { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 8px; background: var(--panel-2); margin-bottom: 4px; }
.doc-icon { font-size: 22px; }
.doc-name { font-weight: 600; overflow-wrap: anywhere; }
.doc-size { font-size: 11px; color: var(--muted); }
.media-note { font-size: 12px; color: var(--muted); font-style: italic; }
.loc { padding: 6px 8px; border-radius: 8px; background: var(--panel-2); margin-bottom: 4px; }
.loc-coords { font-family: ui-monospace, monospace; font-size: 12px; color: var(--muted); }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent-text); cursor: pointer; text-decoration: underline; font-size: 12px; }

/* ---- envio --------------------------------------------------------------------------------- */
.composer { position: relative; background: var(--panel); border-top: 1px solid var(--line); padding: 10px 12px; }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  flex: 1; resize: none; max-height: 180px; min-height: 38px;
  border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 9px 12px; line-height: 1.35;
}
.composer textarea:disabled { opacity: .5; }
.send { height: 38px; }
.closed-note {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 10px; margin-bottom: 8px; border-radius: 8px;
  background: var(--accent-soft); color: var(--accent-text); font-weight: 600; font-size: 13px;
}
.qr-popup {
  position: absolute; left: 12px; right: 12px; bottom: calc(100% + 6px);
  max-height: 300px; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
}
.qr-item { padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.qr-item:last-child { border-bottom: 0; }
.qr-item.active, .qr-item:hover { background: var(--accent-soft); }
.qr-title { font-weight: 700; font-size: 13px; }
.qr-body { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qr-empty { padding: 10px 12px; color: var(--muted); }

/* ---- painel do contato --------------------------------------------------------------------- */
.lead { grid-area: lead; width: 300px; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.lead-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 10px 16px; border-bottom: 1px solid var(--line); }
.lead-head h2, .modal-head h2 { margin: 0; font-size: 15px; }
.lead-body { padding: 14px 16px; overflow-y: auto; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.field textarea { resize: vertical; }
.saved { min-height: 16px; font-size: 12px; color: var(--ok); margin: -4px 0 8px; }
.lead-ad { padding: 10px; border-radius: 8px; background: var(--accent-soft); color: var(--accent-text); font-size: 13px; margin-bottom: 12px; overflow-wrap: anywhere; }
.lead-ad strong { display: block; margin-bottom: 4px; }
.lead-meta { font-size: 12px; color: var(--muted); margin: 0; }
.lead-meta dt { font-weight: 700; margin-top: 6px; }
.lead-meta dd { margin: 0; color: var(--ink); overflow-wrap: anywhere; }

/* ---- modal e avisos ------------------------------------------------------------------------ */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal {
  width: min(640px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column;
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 12px 18px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px 18px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); }
.modal-foot:empty { display: none; }
.modal p { margin: 0 0 12px; line-height: 1.45; }
.modal .hint { color: var(--muted); font-size: 12px; }
.qr-edit { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 12px; }
.qr-edit textarea { min-height: 80px; resize: vertical; margin-top: 8px; }
.qr-edit-actions { display: flex; justify-content: flex-end; gap: 6px; margin-top: 8px; }
.switch { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; font-weight: 600; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }
.tpl-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.tpl-option { text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 8px 10px; cursor: pointer; }
.tpl-option.active { border-color: var(--accent); background: var(--accent-soft); }
.tpl-option:disabled { cursor: not-allowed; opacity: .55; }
.tpl-option small { display: block; color: var(--muted); }
.tpl-preview { white-space: pre-wrap; background: var(--panel-2); border-radius: 8px; padding: 10px; margin-bottom: 12px; line-height: 1.4; }
.file-preview { max-width: 100%; max-height: 300px; display: block; margin: 0 auto 12px; border-radius: 8px; }
.img-full { max-width: 100%; max-height: 75vh; display: block; margin: 0 auto; border-radius: 8px; }

.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 30; }
.toast {
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow);
  font-weight: 600; max-width: min(520px, calc(100vw - 32px));
}
.toast.error { background: var(--accent); color: #fff; }

.only-mobile { display: none; }

@media (max-width: 1100px) {
  .lead { position: fixed; right: 0; top: 56px; bottom: 0; z-index: 10; box-shadow: var(--shadow); }
  .app { grid-template-columns: 300px minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "side"; }
  .topbar { padding: 0 8px; gap: 8px; }
  .topbar-actions { gap: 0; min-width: 0; overflow-x: auto; }
  .topbar .btn { padding: 7px 8px; }
  .chat-head { padding: 8px; gap: 8px; }
  #btn-lead { padding: 7px 8px; }
  .app.in-chat { grid-template-areas: "top" "chat"; }
  .app.in-chat .sidebar { display: none; }
  .app:not(.in-chat) .chat { display: none; }
  .only-mobile { display: inline-block; }
  .brand-sub, #btn-csv { display: none; }
  .window-badge small { display: none; }
  .window-badge { padding: 4px 8px; font-size: 11px; }
  .chat-sub { display: none; }
  .hide-mobile { display: none; }
  .chat-title { min-width: 70px; }
  .messages { padding: 12px; }
  .msg { max-width: 88%; }
  .lead { width: 100%; }
}

/* ---- login --------------------------------------------------------------------------------- */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px 24px; text-align: center;
}
.login-card h1 { font-size: 18px; margin: 0 0 18px; }
.login-card .field { text-align: left; }
.login-card .btn { width: 100%; padding: 10px; }
.login-error { min-height: 18px; color: var(--accent-text); font-size: 13px; margin: 0 0 10px; text-align: left; }
.settings-section { border-top: 1px solid var(--line); margin-top: 16px; padding-top: 14px; }
.settings-section h3 { margin: 0 0 8px; font-size: 14px; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; }
