:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f4;
  --border: #dde1e6;
  --text: #1c2228;
  --muted: #66717d;
  --accent: #0f7a5c;
  --accent-text: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #dcf3e8;
  --thread-bg: #eceee9;
  --read: #1f7ae0;
  --danger: #c0352b;
  --warn: #9a6400;
  --radius: 10px;
  /* gráfico: 3 primeiras cores da paleta categórica validada (claro) */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --meter-track: #cde2fb;
  --meter-fill: #2a78d6;
  --meter-high: #fab219;
  --meter-full: #d03b3b;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1316;
    --surface: #171c20;
    --surface-2: #1f262b;
    --border: #2c353c;
    --text: #e4e8eb;
    --muted: #8d99a4;
    --accent: #2bb38a;
    --accent-text: #06150f;
    --bubble-in: #1f262b;
    --bubble-out: #133a2d;
    --thread-bg: #0c1013;
    --read: #5aa8ff;
    --danger: #ff6b5e;
    --warn: #e0a640;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --grid: #2c353c;
    --axis: #3d474f;
    --meter-track: #184f95;
    --meter-fill: #5598e7;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

button, input, select { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 7px 14px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
button:disabled { opacity: .5; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--text); border-color: var(--border); font-weight: 500; }
button:focus-visible, input:focus-visible, select:focus-visible, li:focus-visible, tr:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

input[type="search"], input[type="password"], select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
}

.muted { color: var(--muted); font-weight: 400; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.error { color: var(--danger); }

/* ---------- Login ---------- */
body.login { align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  display: grid;
  gap: 10px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0 0 8px; }
.login-card button { margin-top: 6px; padding: 10px; }

/* ---------- Barra superior ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
.number-picker select { max-width: 280px; }
.tabs { display: flex; gap: 4px; }
.tabs button { background: transparent; color: var(--muted); border: none; padding: 6px 12px; font-weight: 500; }
.tabs button[aria-current="page"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.stats { margin-left: auto; color: var(--muted); font-size: 12.5px; text-align: right; }
.stats .bad { color: var(--danger); }
.stats .warn { color: var(--warn); }

main { flex: 1; min-height: 0; display: flex; }
.tab { flex: 1; min-width: 0; }
.tab[hidden] { display: none; }

/* ---------- Conversas ---------- */
.chats { display: grid; grid-template-columns: minmax(260px, 340px) 1fr; min-height: 0; }
.chat-list { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.chat-list input { margin: 10px; }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.list li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--surface-2);
  cursor: pointer;
}
.list li:hover { background: var(--surface-2); }
.list li[aria-selected="true"] { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.list .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .time { color: var(--muted); font-size: 12px; }
.list .preview { grid-column: 1 / -1; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.list .empty-item { cursor: default; color: var(--muted); display: block; }

.thread { display: flex; flex-direction: column; min-height: 0; background: var(--thread-bg); }
.thread > .empty { margin: auto; color: var(--muted); }
.thread-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.thread-head .who { display: grid; }
.thread-head .who strong { font-size: 15px; }
.thread-head .window { margin-left: auto; font-size: 12px; color: var(--muted); text-align: right; }
.thread-head .back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 4px; }
.messages .more { align-self: center; margin-bottom: 8px; }
.day { align-self: center; margin: 10px 0 6px; font-size: 12px; color: var(--muted); background: var(--surface); border-radius: 999px; padding: 2px 10px; }

.bubble {
  max-width: min(72%, 560px);
  padding: 7px 10px 5px;
  border-radius: var(--radius);
  background: var(--bubble-in);
  align-self: flex-start;
  box-shadow: 0 1px 1px rgb(0 0 0 / 6%);
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.bubble.out { background: var(--bubble-out); align-self: flex-end; }
.bubble .meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center; margin-top: 2px; font-size: 11px; color: var(--muted); white-space: nowrap; }
.bubble .source { border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; }
.bubble .source.free { color: var(--accent); border-color: var(--accent); }
.bubble .source.paid { color: var(--warn); border-color: var(--warn); }
.bubble .ticks.read { color: var(--read); }
.bubble .ticks.failed { color: var(--danger); }
.bubble .placeholder { color: var(--muted); font-style: italic; }
.bubble img { display: block; max-width: 100%; max-height: 320px; border-radius: 6px; margin-bottom: 4px; }
.bubble audio { display: block; width: min(300px, 60vw); margin-bottom: 4px; }
.bubble video { display: block; max-width: 100%; max-height: 320px; border-radius: 6px; margin-bottom: 4px; }
.bubble a { color: var(--accent); }

.composer { display: flex; gap: 8px; padding: 10px 16px; background: var(--surface); border-top: 1px solid var(--border); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 38px; max-height: 140px; }
.composer-note { padding: 8px 16px; background: var(--surface); border-top: 1px solid var(--border); color: var(--muted); font-size: 13px; }
.composer-note.error { color: var(--danger); }

/* ---------- Contatos e eventos ---------- */
.page { padding: 16px; overflow: auto; min-height: 0; }
.page-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.page-head h2 { margin: 0; font-size: 18px; margin-right: auto; }
.check { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--surface-2); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr[aria-selected="true"] { background: var(--surface-2); }
td.err { color: var(--danger); max-width: 360px; overflow-wrap: anywhere; }
.more { margin: 10px; }
.events { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.event-detail {
  margin: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  font-size: 12px;
  max-height: calc(100vh - 160px);
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ---------- Consumo ---------- */
.dash-info { margin: -4px 0 12px; font-size: 13px; }
.dash-note { font-size: 12px; margin-top: 12px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: grid; gap: 2px; }
.kpi-label { color: var(--muted); font-size: 12.5px; }
.kpi-value { font-size: 24px; font-weight: 600; }
.kpi-detail { color: var(--muted); font-size: 12.5px; }
table.numeric td:not(:first-child), table.numeric th:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }
.number-cell { display: grid; gap: 2px; }
.number-cell .muted { font-size: 12.5px; }
.badge { justify-self: start; font-size: 11px; border: 1px solid var(--border); border-radius: 999px; padding: 0 8px; color: var(--muted); white-space: nowrap; }
.badge.here { color: var(--accent); border-color: var(--accent); }
.meter-cell { display: grid; gap: 4px; justify-items: end; white-space: nowrap; }
.meter { display: block; width: 110px; height: 6px; border-radius: 3px; background: var(--meter-track); overflow: hidden; }
.meter-fill { display: block; height: 100%; background: var(--meter-fill); border-radius: 3px; }
.meter-fill.high { background: var(--meter-high); }
.meter-fill.full { background: var(--meter-full); }
.chart-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-top: 16px; }
.chart-card h3 { margin: 0 0 8px; font-size: 15px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12.5px; color: var(--text); margin-bottom: 6px; }
.legend span, .chart-tip div { display: inline-flex; align-items: center; gap: 6px; }
.chart-tip div { display: flex; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.swatch.s1, .bar.s1 { background: var(--series-1); fill: var(--series-1); }
.swatch.s2, .bar.s2 { background: var(--series-2); fill: var(--series-2); }
.swatch.s3, .bar.s3 { background: var(--series-3); fill: var(--series-3); }
.chart-wrap { position: relative; }
.chart { display: block; width: 100%; height: auto; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover { fill: var(--text); fill-opacity: 0.04; }
.chart-tip { position: absolute; z-index: 2; pointer-events: none; min-width: 170px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; box-shadow: 0 4px 16px rgb(0 0 0 / 12%); display: grid; gap: 3px; }
.chart-tip[hidden] { display: none; }
.tip-total { color: var(--muted); border-top: 1px solid var(--surface-2); padding-top: 3px; margin-top: 2px; }
.chart-table { margin-top: 8px; font-size: 13px; }
.chart-table summary { cursor: pointer; color: var(--muted); }
.chart-table table { margin-top: 6px; }

/* ---------- Configuração do número ---------- */
.cfg-grid { display: grid; grid-template-columns: minmax(260px, 1fr) minmax(0, 2fr); gap: 16px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; min-width: 0; }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.card p { margin: 6px 0; }
.fields { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0; font-size: 13.5px; }
.fields dt { color: var(--muted); }
.fields dd { margin: 0; overflow-wrap: anywhere; }
.url { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px; overflow-wrap: anywhere; }
.destination { font-size: 14px; }
.destination .url { font-size: 13.5px; font-weight: 600; }
.levels { margin: 10px 0; }
.levels th { position: static; font-size: 12.5px; color: var(--muted); white-space: nowrap; background: none; }
.levels tr { cursor: default; }
.levels tr:hover { background: none; }
.levels tr.active { background: var(--surface-2); }
.levels tr.active th { color: var(--text); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 12px 0 6px; }
.actions input { flex: 1 1 260px; min-width: 0; }
button.danger { color: var(--danger); border-color: var(--danger); }
.advanced { margin-top: 10px; }
.advanced summary { cursor: pointer; color: var(--muted); }
.small { font-size: 12px; }
.cfg-status { margin: -4px 0 12px; padding: 8px 12px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border); font-size: 13.5px; overflow-wrap: anywhere; }
.cfg-status.ok { border-color: var(--accent); color: var(--accent); }
.cfg-status.warn { border-color: var(--warn); color: var(--warn); }
.cfg-status.error { border-color: var(--danger); color: var(--danger); }

/* ---------- Telas estreitas ---------- */
@media (max-width: 820px) {
  .topbar { gap: 8px 10px; }
  .stats { width: 100%; text-align: left; margin-left: 0; order: 10; }
  .chats { grid-template-columns: 1fr; }
  .chats .thread { display: none; }
  .chats.show-thread .chat-list { display: none; }
  .chats.show-thread .thread { display: flex; }
  .thread-head .back { display: inline-block; }
  .bubble { max-width: 88%; }
  .events { grid-template-columns: 1fr; }
  .cfg-grid { grid-template-columns: 1fr; }
}
