:root {
  --green: #128c7e; --green-dark: #075e54; --green-light: #dcf8c6;
  --bg: #eef1f4; --panel: #fff; --line: #e3e6ea; --text: #1f2933; --muted: #6b7785;
  --danger: #c0392b; --ai: #6d4bd8;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); height: 100vh; height: 100dvh; overflow: hidden; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1em; }
.row { display: flex; gap: 6px; align-items: center; }
.wrap { flex-wrap: wrap; }
input, textarea, select { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: inherit; }
textarea { resize: vertical; }
.btn { font: inherit; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: #fff; cursor: pointer; white-space: nowrap; }
.btn:hover { filter: brightness(.96); }
.btn:disabled { opacity: .6; cursor: wait; }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.ai { color: var(--ai); border-color: #d8cdfa; }
.btn.small { padding: 4px 8px; font-size: 13px; }
.icon { background: none; border: 0; font-size: 18px; cursor: pointer; padding: 6px; border-radius: 6px; color: inherit; }
.icon:hover { background: rgba(0,0,0,.06); }

/* login */
.login { height: 100vh; height: 100dvh; display: grid; place-items: center; background: var(--green-dark); }
.card { background: #fff; padding: 32px; border-radius: 14px; width: min(360px, 92vw); display: grid; gap: 12px; }
.card h1 { margin: 0; color: var(--green-dark); }

/* layout */
.app { display: grid; grid-template-columns: 360px 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; background: var(--green-dark); color: #fff; }
.side-head strong { display: block; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 10px; background: #f5b041; color: #222; }
.badge.ok { background: #a9dfbf; }
.tabs { display: flex; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button { flex: 1; background: none; border: 0; padding: 10px 6px; cursor: pointer; font: inherit; font-size: 14px; color: var(--muted); border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--green-dark); border-color: var(--green); font-weight: 600; }
.search { margin: 10px; }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-list li { display: flex; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.chat-list li:hover, .chat-list li.active { background: #f0f7f5; }
.chat-list li.none { cursor: default; justify-content: center; }
.avatar { width: 42px; height: 42px; border-radius: 50%; background: #cfd8dc; display: grid; place-items: center; font-weight: 600; color: #455a64; flex-shrink: 0; }
.li-body { flex: 1; min-width: 0; }
.li-top, .li-bottom { display: flex; justify-content: space-between; gap: 8px; }
.li-top small { color: var(--muted); }
.preview { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread { background: #25d366; color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; font-weight: 600; height: 20px; line-height: 20px; }
.assigned { color: var(--green); font-size: 12px; }

/* conversa */
.conversation { display: flex; min-height: 0; min-width: 0; }
.empty { margin: auto; color: var(--muted); }
.conv { display: flex; flex-direction: column; flex: 1; min-height: 0; min-width: 0; }
.conv-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.conv-title { flex: 1; min-width: 160px; display: flex; flex-direction: column; }
.conv-title strong { cursor: text; }
.back { display: none; }
.summary { background: #f4f0ff; border-bottom: 1px solid #e2d9ff; padding: 10px 40px 10px 16px; position: relative; font-size: 14px; line-height: 1.5; }
.summary .icon { position: absolute; right: 8px; top: 4px; font-size: 14px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 6px; background: #e5ddd5; }
.msg { max-width: min(70%, 560px); padding: 6px 10px 4px; border-radius: 8px; background: #fff; box-shadow: 0 1px 1px rgba(0,0,0,.08); word-wrap: break-word; }
.msg.out { align-self: flex-end; background: var(--green-light); }
.msg.in { align-self: flex-start; }
.msg.system { align-self: center; background: #fff8d6; font-size: 12px; color: #6b5d1f; padding: 3px 10px; }
.msg .who { font-size: 12px; font-weight: 600; color: var(--green-dark); }
.msg .ts { font-size: 11px; color: var(--muted); text-align: right; }
.msg img, .msg video { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; }
.msg img.sticker { max-width: 140px; }
.msg audio { max-width: 100%; }
.msg .doc { display: inline-block; padding: 8px; background: rgba(0,0,0,.05); border-radius: 6px; color: inherit; }
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px; display: grid; gap: 6px; }
.composer-tools, .composer-main { display: flex; gap: 6px; align-items: center; }
.composer-tools input { flex: 1; min-width: 0; font-size: 13px; }
.composer-tools select { max-width: 180px; font-size: 13px; }
.composer-main textarea { flex: 1; min-height: 44px; max-height: 200px; }
.attach { font-size: 20px; }
.file-chip { font-size: 13px; color: var(--muted); }

/* modais */
.dlg { border: 0; border-radius: 12px; padding: 0; width: min(760px, 94vw); max-height: 90vh; }
.dlg.small { width: min(420px, 94vw); }
.dlg::backdrop { background: rgba(0,0,0,.4); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.pane, .dlg > form { padding: 16px; }
.grid { display: grid; gap: 10px; }
.grid label { display: grid; gap: 6px; }
.grid label.check { display: flex; align-items: center; gap: 8px; }
.qr { width: 260px; height: 260px; display: block; margin: 10px 0; }
.table { width: 100%; border-collapse: collapse; margin-bottom: 16px; font-size: 14px; }
.table th, .table td { text-align: left; padding: 6px; border-bottom: 1px solid var(--line); }
.table tr.inactive { opacity: .5; }
.plain { list-style: none; padding: 0; margin: 0 0 16px; }
.plain li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); white-space: pre-wrap; }
body:not(.is-admin) #adminTabs button:not([data-pane=panePass]) { display: none; }

/* celular */
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .conversation { position: fixed; inset: 0; z-index: 5; background: var(--bg); transform: translateX(100%); transition: transform .2s; }
  .conversation.open { transform: none; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
  .messages { padding: 12px; }
  .composer-tools { flex-wrap: wrap; }
}

/* notas internas */
.msg.note { align-self: center; max-width: min(80%, 560px); background: #fff3c4; border: 1px dashed #d4a72c; }
.msg.note .who { color: #8a5a00; }
.note-toggle[aria-pressed="true"] { background: #fff3c4; border-color: #d4a72c; color: #6b4500; }
.composer.note-mode textarea { background: #fffbea; border-color: #d4a72c; }
.note-hint { font-size: 12px; color: #6b4500; background: #fff3c4; padding: 4px 8px; border-radius: 6px; }

/* chat da equipe */
.team-btn { position: relative; }
.tbadge { position: absolute; top: 0; right: -2px; background: #25d366; color: #fff; border-radius: 9px; min-width: 18px; height: 18px; font-size: 11px; line-height: 18px; font-weight: 700; padding: 0 4px; }
.team-side { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.team-side-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.team-msgs { background: #eef1f4; }
.msg.team-in { align-self: flex-start; background: #fff; }
.msg.team-out { align-self: flex-end; background: #d9ecff; }
.avatar.geral { background: var(--green); color: #fff; }

/* iPhone (app instalado): o topo e a barra de digitar ficam fora da área da hora/bateria e da barra inferior.
   Fica no fim do arquivo para não ser anulado pelas regras de cima. */
.app .side-head { padding-top: max(12px, env(safe-area-inset-top, 0px)); padding-left: max(14px, env(safe-area-inset-left, 0px)); padding-right: max(14px, env(safe-area-inset-right, 0px)); }
.app .conv-head { padding-top: max(10px, env(safe-area-inset-top, 0px)); }
.app .composer { padding-bottom: max(8px, env(safe-area-inset-bottom, 0px)); }
.app .chat-list { padding-bottom: env(safe-area-inset-bottom, 0px); }
@media (min-width: 801px) { .app .conv-head { padding-top: 10px; } }

/* setores, robô e backup */
.pane-title { margin: 8px 0 0; font-size: 15px; color: var(--green-dark); }
.small-label { font-size: 13px; margin-bottom: 4px; }
.dept-list li { align-items: center; padding: 6px 0; white-space: normal; }
.dept-list .num { font-weight: 700; width: 22px; }
.dept-list input[type=text] { flex: 1; min-width: 0; }
.menu-preview { white-space: pre-wrap; background: var(--green-light); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.menu-preview::before { content: "Prévia do menu"; display: block; font-size: 11px; font-weight: 700; color: var(--green-dark); margin-bottom: 4px; }
.dept-chip { display: inline-block; font-size: 11px; background: #e6f2ef; color: var(--green-dark); border-radius: 10px; padding: 0 7px; margin-right: 4px; }
.dept-toggle { font: inherit; font-size: 11px; border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 1px 7px; margin: 2px; cursor: pointer; color: var(--muted); }
.dept-toggle.on { background: var(--green); border-color: var(--green); color: #fff; }
.steps-list { margin: 0; padding-left: 20px; line-height: 1.6; }
.steps-list li { margin-bottom: 2px; }
.waiting-bot { font-size: 11px; color: #8a5a00; }

/* etiquetas e ficha */
.tag-row { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { display: inline-block; font-size: 11px; border-radius: 10px; padding: 1px 8px; color: #fff; font-weight: 600; line-height: 16px; }
.tag-toggle { font: inherit; font-size: 12px; border-radius: 12px; padding: 3px 10px; border: 2px solid var(--c); background: #fff; color: var(--c); cursor: pointer; font-weight: 600; }
.tag-toggle.on { background: var(--c); color: #fff; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 480px) { .two { grid-template-columns: 1fr; } }
.client-meta { font-size: 11px; color: var(--muted); }
/* espera */
.wait { font-size: 11px; font-weight: 700; color: #8a5a00; background: #fff3c4; border-radius: 10px; padding: 0 6px; margin-left: 4px; }
.wait.late { color: #fff; background: var(--danger); }
/* citação */
.msg .reply-btn { float: right; margin: -2px -4px 0 6px; font-size: 13px; opacity: .45; background: none; border: 0; cursor: pointer; padding: 2px 4px; }
.msg:hover .reply-btn, .msg .reply-btn:focus { opacity: 1; }
.quote { border-left: 3px solid var(--green); background: rgba(0,0,0,.05); border-radius: 4px; padding: 3px 6px; font-size: 12px; color: var(--muted); margin-bottom: 3px; max-height: 3.2em; overflow: hidden; }
.reply-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; border-left: 3px solid var(--green); background: #f0f7f5; padding: 4px 8px; border-radius: 6px; font-size: 13px; }
.reply-bar span { color: var(--muted); }
/* atalho / */
.composer { position: relative; }
.slash-menu { position: absolute; bottom: 100%; left: 12px; right: 12px; max-height: 240px; overflow-y: auto; list-style: none; margin: 0 0 4px; padding: 4px; background: #fff; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.12); z-index: 3; }
.slash-menu li { padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.slash-menu li.sel, .slash-menu li:hover { background: #e6f2ef; }
.slash-menu li b { color: var(--green-dark); }
.slash-menu li span { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* agendamento */
.sched-count { background: var(--green); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 11px; }
/* relatórios */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; margin: 10px 0; }
.kpi { background: #f5f7f8; border-radius: 8px; padding: 8px 10px; }
.kpi b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.kpi span { font-size: 12px; color: var(--muted); }
.kpi.alert b { color: var(--danger); }
.table-wrap { overflow-x: auto; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.stars { display: grid; gap: 4px; max-width: 420px; }
.stars div { display: grid; grid-template-columns: 34px 1fr 40px; align-items: center; gap: 8px; font-size: 13px; }
.stars i { display: block; height: 10px; border-radius: 5px; background: var(--green); min-width: 2px; }

/* fiscalização */
.sup-rules { display: grid; gap: 8px; }
.sup-rules label { display: block !important; }
.sup-rules input[type=number] { width: 64px; padding: 4px 6px; }
.score { display: inline-block; min-width: 34px; text-align: center; border-radius: 10px; font-weight: 700; font-size: 12px; padding: 1px 6px; color: #fff; }
.score.good { background: #0e6b5c; } .score.mid { background: #b8860b; } .score.bad { background: var(--danger); } .score.none { background: #9aa5ae; }
.sup-detail td { background: #f7f9fa; font-size: 13px; }
.sup-detail ul { margin: 4px 0; padding-left: 18px; }
#supTable tr.sup-row { cursor: pointer; }
#supTable tr.sup-row:hover td { background: #f0f7f5; }

/* Nunca mais largo que a tela: nomes e textos compridos são cortados com "…" */
.app { grid-template-columns: 360px minmax(0, 1fr); }
.sidebar, .conversation, .conv { min-width: 0; }
.side-head > div:first-child { min-width: 0; flex: 1; }
.side-head > div:first-child strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-head .row { flex-shrink: 0; }
.li-top { min-width: 0; }
.li-top strong { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.li-top small { flex-shrink: 0; white-space: nowrap; }
.li-bottom { min-width: 0; }
.conv-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 800px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .side-head .icon { padding: 6px 4px; }
}

/* ===== Menu lateral (computador) / barra de abas (celular) ===== */
.app { grid-template-columns: 84px 340px minmax(0, 1fr); }
.rail { background: var(--green-dark); display: flex; flex-direction: column; gap: 2px; min-height: 0; overflow-y: auto;
  padding: max(10px, env(safe-area-inset-top, 0px)) 6px max(10px, env(safe-area-inset-bottom, 0px)) max(6px, env(safe-area-inset-left, 0px)); }
.rail-brand { width: 40px; height: 40px; border-radius: 12px; background: #25d366; color: #fff; font-weight: 800; font-size: 20px; display: grid; place-items: center; margin: 0 auto 10px; flex-shrink: 0; }
.rail-group { display: flex; flex-direction: column; gap: 2px; }
.rail-btn { position: relative; display: flex; flex-direction: column; align-items: center; gap: 3px; background: none; border: 0; color: #cfe7e2;
  padding: 8px 2px; border-radius: 10px; cursor: pointer; font: inherit; min-width: 0; }
.rail-btn .ri { font-size: 20px; line-height: 1; }
.rail-btn .rl { font-size: 11px; line-height: 1.1; white-space: nowrap; }
.rail-btn:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.rail-btn:focus-visible { outline: 2px solid #fff; outline-offset: -2px; }
.rail-btn.active { background: rgba(255, 255, 255, .18); color: #fff; font-weight: 600; }
.rail-btn .rc { position: absolute; top: 3px; right: 6px; background: #25d366; color: #fff; border-radius: 9px; min-width: 18px; height: 18px;
  font-size: 11px; line-height: 18px; padding: 0 4px; font-weight: 700; text-align: center; }
.rail-btn .rc:empty, .rail-btn .rc.hidden { display: none; }
.rail-spacer { flex: 1; min-height: 8px; }
.mob-only { display: none !important; }
.side-head { background: var(--panel); color: var(--text); border-bottom: 1px solid var(--line); }
.side-title { min-width: 0; }
.side-title strong { display: block; font-size: 18px; color: var(--green-dark); }
.side-title small { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 4px; min-width: 0; }
.side-title small #meName { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app .side-head { padding-top: max(12px, env(safe-area-inset-top, 0px)); }
.sheet-list { display: grid; padding: 8px; }
.sheet-list button { text-align: left; font: inherit; font-size: 16px; background: none; border: 0; border-bottom: 1px solid var(--line); padding: 14px 10px; cursor: pointer; }
.sheet-list button:last-child { border-bottom: 0; }
.sheet-list button:hover { background: #f0f7f5; }

@media (max-width: 800px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); }
  .rail { position: fixed; left: 0; right: 0; bottom: 0; z-index: 4; flex-direction: row; align-items: stretch; overflow: visible; gap: 0;
    padding: 4px max(4px, env(safe-area-inset-right, 0px)) max(4px, env(safe-area-inset-bottom, 0px)) max(4px, env(safe-area-inset-left, 0px));
    box-shadow: 0 -2px 10px rgba(0, 0, 0, .12); }
  .rail-brand, .rail-spacer, .desk-only { display: none !important; }
  .mob-only { display: flex !important; }
  .rail-group { flex-direction: row; flex: 3; gap: 0; }
  .rail-btn { flex: 1; padding: 6px 2px; }
  .rail-group .rail-btn { flex: 1; }
  .sidebar { padding-bottom: calc(60px + env(safe-area-inset-bottom, 0px)); border-right: 0; }
  /* conversa aberta: cabeçalho enxuto, ações no menu ⋯ */
  .conv-head { position: relative; flex-wrap: nowrap; }
  #btnActions { font-size: 24px; padding: 4px 10px; }
  .conv-actions { display: none !important; position: absolute; top: calc(100% - 4px); right: 8px; z-index: 6; flex-direction: column; align-items: stretch;
    background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 8px; gap: 6px; min-width: 220px; box-shadow: 0 8px 24px rgba(0, 0, 0, .15); }
  .conv-actions.open { display: flex !important; }
  .conv-actions > * { width: 100%; text-align: left; }
  /* ferramentas de escrita atrás do ＋ */
  .composer-tools { display: none; flex-wrap: wrap; }
  .composer.tools-open .composer-tools { display: flex; }
  .composer-tools > * { flex: 1 1 45%; }
  .composer-tools #aiInstruction { flex-basis: 100%; }
  .tools-btn { font-size: 22px; }
  .composer-main textarea { min-width: 0; }
}

/* Barra de digitar sempre dentro da tela */
.composer { grid-template-columns: minmax(0, 1fr); }
.composer-tools { flex-wrap: wrap; min-width: 0; }
.composer-main { min-width: 0; }
.composer-main textarea { flex: 1 1 auto; min-width: 0; width: 100%; }
@media (max-width: 800px) {
  .composer { padding-left: max(8px, env(safe-area-inset-left, 0px)); padding-right: max(8px, env(safe-area-inset-right, 0px)); }
  .composer-main { gap: 4px; }
  .composer-main .icon { padding: 6px 4px; flex-shrink: 0; }
  .composer-main .btn.primary { padding: 10px 12px; flex-shrink: 0; }
  .composer-main textarea { font-size: 16px; }
  .composer-tools > * { flex: 1 1 calc(50% - 6px); min-width: 0; max-width: none !important; }
  .composer-tools #aiInstruction { flex-basis: 100%; }
}
@media (max-width: 800px) {
  .composer-tools .btn { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; padding: 8px 6px; }
}
