:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --line: #e3e8ef;
  --text: #1b2430;
  --muted: #6b7686;
  --brand: #0f766e;
  --brand-2: #14b8a6;
  --red: #dc2626;
  --red-bg: #fde8e8;
  --yellow: #d97706;
  --yellow-bg: #fef3c7;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --note-bg: #fff8db;
  --out-bg: #dff7f2;
  --in-bg: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e141b; --panel: #151d26; --panel-2: #1a2430; --line: #263241; --text: #e6edf3; --muted: #8b98a8;
    --red-bg: #3b1616; --yellow-bg: #3a2a0b; --green-bg: #0f2e1b; --note-bg: #33300f; --out-bg: #0f3330; --in-bg: #1d2834;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); min-height: 1em; }
h1 { font-size: 20px; margin: 0 0 16px; }
h2 { font-size: 15px; margin: 0 0 10px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; }
textarea { resize: vertical; }
label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--brand-2); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: #115e59; }
.btn.danger { color: var(--red); border-color: var(--red); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--panel); padding: 28px; border-radius: 14px; box-shadow: var(--shadow); display: grid; gap: 14px; width: min(360px, 100%); }
.brand { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.brand.big { font-size: 20px; margin-bottom: 8px; }
.logo { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; font-weight: 700; }

/* Layout */
.app { display: grid; grid-template-columns: 210px 1fr; height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 10px; gap: 2px; }
.sidebar .brand { padding: 4px 8px 16px; }
.sidebar a { color: var(--text); text-decoration: none; padding: 8px 10px; border-radius: 8px; display: flex; gap: 8px; }
.sidebar a:hover { background: var(--panel-2); }
.sidebar a.active { background: var(--out-bg); color: var(--brand); font-weight: 600; }
.sidebar-foot { margin-top: auto; display: grid; gap: 6px; padding: 10px 8px 0; border-top: 1px solid var(--line); }
.switch { display: flex; flex-direction: row; align-items: center; gap: 8px; font-size: 13px; color: var(--text); cursor: pointer; }
.switch input { width: auto; }
.me { font-weight: 600; }
.view { overflow: hidden; height: 100vh; }
.page { padding: 24px; overflow: auto; height: 100%; }

/* Chamados: 3 colunas */
.inbox { display: grid; grid-template-columns: 340px 1fr 320px; height: 100%; }
.list-col { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.tabs { display: flex; flex-wrap: wrap; gap: 4px; padding: 10px; border-bottom: 1px solid var(--line); }
.tab { font-size: 12px; padding: 4px 9px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; color: var(--text); }
.tab.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.tab .count { margin-left: 4px; opacity: .8; }
.tab.alert:not(.active) { border-color: var(--red); color: var(--red); }
.filters { display: flex; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.filters select { width: auto; }
.ticket-list { overflow: auto; flex: 1; }
.ticket-item { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; grid-template-columns: 8px 1fr; gap: 10px; }
.ticket-item:hover { background: var(--panel-2); }
.ticket-item.active { background: var(--out-bg); }
.ticket-item .bar { border-radius: 4px; }
.ti-top { display: flex; justify-content: space-between; gap: 6px; }
.ti-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ti-msg { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ti-meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.bg-vermelho { background: var(--red); } .bg-amarelo { background: var(--yellow); } .bg-verde { background: var(--green); }
.pill { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; padding: 1px 8px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.pill.vermelho { background: var(--red-bg); color: var(--red); border-color: transparent; font-weight: 600; }
.pill.amarelo { background: var(--yellow-bg); color: var(--yellow); border-color: transparent; font-weight: 600; }
.pill.verde { background: var(--green-bg); color: var(--green); border-color: transparent; font-weight: 600; }
.pill.grave { background: var(--red); color: #fff; border-color: transparent; font-weight: 700; }
.pill.late { color: var(--red); border-color: var(--red); }
.pill.ok { color: var(--green); }

/* Conversa */
.chat-col { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel); display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.chat-head .title { font-weight: 600; font-size: 15px; }
.chat-body { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 72%; padding: 8px 12px; border-radius: 12px; box-shadow: var(--shadow); white-space: pre-wrap; word-wrap: break-word; }
.msg.in { background: var(--in-bg); align-self: flex-start; border-top-left-radius: 3px; }
.msg.out { background: var(--out-bg); align-self: flex-end; border-top-right-radius: 3px; }
.msg.system { background: transparent; box-shadow: none; border: 1px dashed var(--line); align-self: flex-end; color: var(--muted); font-size: 12px; }
.msg.note { background: var(--note-bg); align-self: center; max-width: 85%; border: 1px solid #f0d97a; }
.msg .meta { font-size: 11px; color: var(--muted); margin-top: 4px; }
.event { align-self: center; font-size: 11px; color: var(--muted); }
.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 10px 12px; display: grid; gap: 8px; }
.composer.note-mode textarea { background: var(--note-bg); }
.composer-tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.composer-tools .spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 5px 10px; font: inherit; font-size: 12px; cursor: pointer; color: var(--text); }
.seg button.active { background: var(--brand); color: #fff; }
.qr-menu { position: relative; }
.qr-pop { position: absolute; bottom: 110%; left: 0; width: 320px; max-height: 280px; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); z-index: 5; }
.qr-pop div { padding: 8px 10px; cursor: pointer; border-bottom: 1px solid var(--line); }
.qr-pop div:hover { background: var(--panel-2); }

/* Detalhes */
.detail-col { border-left: 1px solid var(--line); background: var(--panel); overflow: auto; padding: 14px; display: grid; gap: 14px; align-content: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 8px; }
.ai-card { border-color: var(--brand-2); background: linear-gradient(180deg, var(--out-bg), var(--panel) 60%); }
.prio-buttons { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.prio-buttons button { border: 2px solid transparent; border-radius: 8px; padding: 8px 4px; cursor: pointer; font: inherit; font-weight: 600; font-size: 12px; opacity: .55; }
.prio-buttons button.active { opacity: 1; border-color: currentColor; }
.prio-buttons .vermelho { background: var(--red-bg); color: var(--red); }
.prio-buttons .amarelo { background: var(--yellow-bg); color: var(--yellow); }
.prio-buttons .verde { background: var(--green-bg); color: var(--green); }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { flex: 1; }
.kv { display: grid; grid-template-columns: 90px 1fr; gap: 4px 8px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.hist-item { font-size: 12px; padding: 6px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.hist-item:last-child { border: 0; }

/* Painel */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.kpi .v { font-size: 26px; font-weight: 700; }
.kpi .l { color: var(--muted); font-size: 12px; }
.kpi.vermelho .v { color: var(--red); } .kpi.amarelo .v { color: var(--yellow); } .kpi.verde .v { color: var(--green); }
table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: 13px; }
th { background: var(--panel-2); color: var(--muted); font-weight: 600; font-size: 12px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; align-items: end; }

.toast { position: fixed; bottom: 20px; right: 20px; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); z-index: 50; max-width: 360px; }
.toast.err { background: var(--red); color: #fff; }

.back-btn { display: none; }
@media (max-width: 1200px) { .inbox { grid-template-columns: 300px 1fr; } .detail-col { display: none; } .inbox.show-detail .detail-col { display: grid; position: fixed; right: 0; top: 0; bottom: 0; width: 330px; z-index: 10; box-shadow: var(--shadow); } }
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .sidebar { order: 2; flex-direction: row; padding: 4px; overflow-x: auto; border-right: 0; border-top: 1px solid var(--line); }
  .sidebar .brand, .sidebar-foot, .sidebar a span { display: none; }
  .view { height: calc(100vh - 52px); }
  .page { padding: 16px; }
  .inbox { grid-template-columns: 1fr; }
  .inbox.has-ticket .list-col { display: none; }
  .inbox:not(.has-ticket) .chat-col { display: none; }
  .back-btn { display: inline-block; }
  .chat-head > .row { flex: 1 1 100% !important; justify-content: flex-start; }
  .msg { max-width: 88%; }
}
