:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --pos: #16a34a;
  --neg: #dc2626;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #2a2f38;
    --primary: #3b82f6;
    --pos: #22c55e;
    --neg: #f87171;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
header {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px 24px;
  padding: 14px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 12px; }
h2 small { color: var(--muted); font-weight: 400; }
nav { display: flex; gap: 4px; flex: 1; }
.tab {
  border: 0; background: none; color: var(--muted); padding: 8px 12px; border-radius: 8px; cursor: pointer; font: inherit;
}
.tab:hover { background: var(--bg); }
.tab.ativo { color: var(--text); background: var(--bg); font-weight: 600; }
.mes { display: flex; align-items: center; gap: 8px; color: var(--muted); }

main { max-width: 1200px; margin: 0 auto; padding: 24px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 16px; }
.card, .bloco { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card span { color: var(--muted); font-size: 13px; }
.card strong { display: block; font-size: 26px; margin-top: 4px; font-variant-numeric: tabular-nums; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

.grade { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.largo { grid-column: 1 / -1; }
@media (max-width: 800px) {
  .grade { grid-template-columns: 1fr; }
  header, main { padding-left: 16px; padding-right: 16px; }
}
.grafico { position: relative; height: 280px; }

form label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
input, select, button { font: inherit; color: inherit; }
form input:not([type=radio]):not([type=color]), form select, .mes input, .tabela select {
  display: block; width: 100%; margin-top: 4px; padding: 8px 10px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
}
.mes input { width: auto; margin: 0; }
input[type=color] { display: block; margin-top: 4px; width: 48px; height: 34px; border: 1px solid var(--border); border-radius: 8px; background: none; padding: 2px; }
.segmentado { display: flex; gap: 8px; margin-bottom: 12px; }
.segmentado label {
  flex: 1; margin: 0; padding: 8px; text-align: center; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; color: var(--text);
}
.segmentado input { position: absolute; opacity: 0; pointer-events: none; }
.segmentado label:has(input:checked) { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, transparent); font-weight: 600; }
.segmentado label:has(input:focus-visible) { outline: 2px solid var(--primary); }

.acoes { display: flex; gap: 8px; }
button { padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; }
button:hover { background: var(--bg); }
button.primario { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primario:hover { filter: brightness(1.08); }
button.icone { padding: 4px 8px; border: 0; background: none; color: var(--muted); }
button.icone:hover { color: var(--text); background: var(--bg); }

.dica { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.arquivo input { font-size: 13px; }

.rolagem { overflow-x: auto; }
.tabela { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.tabela th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 8px; border-bottom: 1px solid var(--border); }
.tabela td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tabela tr:last-child td { border-bottom: 0; }
.tabela .num { text-align: right; white-space: nowrap; }
.tabela select { margin: 0; padding: 4px 6px; min-width: 140px; }
.tabela .desc { max-width: 360px; overflow-wrap: anywhere; }
.compacta td { padding: 6px 4px; }
.bolinha { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; vertical-align: middle; }
.vazio { color: var(--muted); text-align: center; padding: 24px 0; margin: 0; }

.lista-cats { list-style: none; margin: 0 0 20px; padding: 0; }
.lista-cats li { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.lista-cats li input[type=text] { flex: 1; padding: 6px 8px; background: transparent; border: 1px solid transparent; border-radius: 6px; }
.lista-cats li input[type=text]:hover, .lista-cats li input[type=text]:focus { border-color: var(--border); background: var(--bg); }
.lista-cats li input[type=color] { margin: 0; width: 32px; height: 28px; }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgb(0 0 0 / .2);
  max-width: calc(100% - 32px);
}
#toast.erro { background: var(--neg); color: #fff; }

.ultima {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  margin-top: 8px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); font-size: 13px;
}
.ultima span { flex: 1; min-width: 200px; }
.ultima button { padding: 6px 12px; }

dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 20px; width: min(460px, calc(100% - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog p { margin: 0 0 8px; color: var(--muted); }
dialog ul { margin: 0 0 16px; padding-left: 20px; font-variant-numeric: tabular-nums; }
dialog .acoes { flex-wrap: wrap; }

/* ---------- Cabeçalho com empresas ---------- */
header { flex-direction: column; align-items: stretch; gap: 10px; }
.topo { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 24px; }
.topo h1 { flex: 1; }
.unidades { display: flex; gap: 4px; padding: 3px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; flex-wrap: wrap; }
.unidades button { border: 0; background: none; padding: 7px 14px; border-radius: 7px; color: var(--muted); font-weight: 500; }
.unidades button:hover { color: var(--text); background: none; }
.unidades button.ativo { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgb(0 0 0 / .12); }
.muted { color: var(--muted); }

/* ---------- Botão de categoria ---------- */
.campo { margin-bottom: 12px; font-size: 13px; color: var(--muted); }
.cat-btn {
  display: inline-flex; align-items: center; gap: 0; max-width: 100%; padding: 4px 8px;
  background: transparent; border: 1px solid transparent; border-radius: 6px; text-align: left; font-size: 14px;
}
.cat-btn:hover, .cat-btn[aria-expanded=true] { border-color: var(--border); background: var(--bg); }
.cat-btn.cheio { display: flex; width: 100%; margin-top: 4px; padding: 8px 10px; background: var(--bg); border-color: var(--border); border-radius: 8px; color: var(--text); }
.cat-rotulo { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-rotulo.sem { color: var(--muted); font-style: italic; }
.cat-seta { color: var(--muted); margin-left: 8px; font-size: 12px; }

/* ---------- Seletor de categorias (cascata) ---------- */
.seletor {
  position: absolute; z-index: 20; display: grid; grid-template-columns: 1fr 1fr;
  width: min(520px, calc(100vw - 16px)); max-height: 340px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 12px 32px rgb(0 0 0 / .18); overflow: hidden;
}
.seletor[hidden] { display: none; }
.seletor-col { overflow-y: auto; padding: 6px; max-height: 340px; }
#sel-filhos { border-left: 1px solid var(--border); background: var(--bg); }
.sel-item {
  display: flex; align-items: center; width: 100%; padding: 7px 10px; border: 0; border-radius: 6px;
  background: none; text-align: left; font-size: 14px;
}
.sel-item:hover, .sel-item:focus-visible { background: var(--bg); outline: none; }
#sel-filhos .sel-item:hover, #sel-filhos .sel-item:focus-visible { background: var(--surface); }
.sel-item.aberto { background: color-mix(in srgb, var(--primary) 12%, transparent); }
.sel-item.atual .sel-nome { font-weight: 600; color: var(--primary); }
.sel-nome { flex: 1; overflow-wrap: anywhere; }
.sel-item em { color: var(--muted); font-size: 13px; }
.sel-titulo { margin: 4px 10px 6px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.sel-dica { margin: 12px 10px; font-size: 13px; color: var(--muted); }

/* ---------- Dashboard: detalhe com subcategorias ---------- */
.detalhe { max-height: 360px; overflow-y: auto; }
.linha-cat td { font-weight: 600; }
.linha-sub td { color: var(--muted); font-size: 13px; border-bottom-style: dashed; }
.linha-sub td:first-child { padding-left: 26px; }
.compacta th { padding-top: 14px; }

/* ---------- Configurações ---------- */
.col { display: flex; flex-direction: column; gap: 16px; }
.lista-cats li input[type=text] { flex: 1; }
.arvore, .arvore ul { list-style: none; margin: 0; padding: 0; }
.arvore > li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.arvore > li:last-child { border-bottom: 0; }
.arvore ul { margin: 2px 0 4px 40px; }
.arvore .linha { display: flex; align-items: center; gap: 6px; }
.arvore input[type=text] {
  flex: 1; min-width: 0; padding: 5px 8px; background: transparent; border: 1px solid transparent; border-radius: 6px;
}
.arvore .no-pai > .linha input[type=text] { font-weight: 600; }
.arvore ul input[type=text] { font-size: 14px; color: var(--muted); }
.arvore input[type=text]:hover, .arvore input[type=text]:focus { border-color: var(--border); background: var(--bg); color: var(--text); }
.arvore input[type=color] { margin: 0; width: 30px; height: 28px; }

/* ---------- Sugestões de categoria ---------- */
.barra {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-bottom: 12px; padding: 10px 12px;
  border: 1px solid color-mix(in srgb, var(--primary) 35%, var(--border)); border-radius: 8px;
  background: color-mix(in srgb, var(--primary) 7%, var(--surface)); font-size: 13px;
}
.barra span { flex: 1; min-width: 220px; }
.barra button { padding: 6px 12px; }
.barra button:disabled { opacity: .6; cursor: progress; }
.cel-cat { display: flex; align-items: center; gap: 2px; }
.selo {
  margin-left: 8px; padding: 1px 6px; border-radius: 999px; font-size: 11px; font-weight: 600; white-space: nowrap;
  background: color-mix(in srgb, var(--muted) 18%, transparent); color: var(--muted);
}
.selo-ia { background: color-mix(in srgb, #8b5cf6 18%, transparent); color: #7c3aed; }
@media (prefers-color-scheme: dark) { .selo-ia { color: #c4b5fd; } }
.tem-sugestao .cat-rotulo { font-style: italic; }
button.confirmar { color: var(--pos); font-weight: 700; }
button.confirmar:hover { color: var(--pos); background: color-mix(in srgb, var(--pos) 12%, transparent); }

/* ---------- Login, sair, exportar ---------- */
.pagina-login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.caixa-login { width: min(360px, 100%); }
.caixa-login h1 { margin-bottom: 20px; }
.caixa-login .acoes button { width: 100%; }
.erro-login { color: var(--neg); font-size: 13px; margin: -4px 0 12px; }
.sair { padding: 6px 12px; font-size: 13px; color: var(--muted); }
.titulo-lista { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 8px 16px; margin-bottom: 12px; }
.titulo-lista h2 { margin: 0; }
.botao {
  display: inline-block; padding: 7px 14px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); text-decoration: none; font-size: 14px; white-space: nowrap;
}
.botao:hover { background: var(--bg); }

/* ---------- Pessoal ---------- */
.unidades button.pessoal { margin-left: 10px; position: relative; }
.unidades button.pessoal::before {
  content: ''; position: absolute; left: -7px; top: 6px; bottom: 6px; width: 1px; background: var(--border);
}
.linha-retirada td { background: color-mix(in srgb, var(--muted) 6%, transparent); }
.linha-retirada small { font-size: 12px; }
.cat-fixa { display: inline-flex; align-items: center; padding: 4px 8px; font-size: 14px; color: var(--muted); }
.linha-transf td { color: var(--muted); }
.linha-transf .num { text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--muted) 60%, transparent); }
.segmentado.escopo { margin-bottom: 8px; }
.arvore .selo { margin-left: 0; }
.arvore .papel-sub { width: auto; flex: none; font-size: 13px; padding: 4px 6px; }

/* ---------- Celular ---------- */
html { -webkit-text-size-adjust: 100%; }
button, a, input, select, label { -webkit-tap-highlight-color: transparent; }

@media (max-width: 640px) {
  /* 16px nos campos: abaixo disso o iPhone dá zoom ao tocar */
  input, select, textarea, .cat-btn.cheio { font-size: 16px; }

  /* Topo mais baixo e sem ficar fixo, para sobrar tela */
  header { position: static; padding: 12px 16px; gap: 8px; }
  h1 { font-size: 17px; }
  .topo { gap: 8px 12px; }
  .unidades { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; scrollbar-width: none; }
  .unidades::-webkit-scrollbar { display: none; }
  .unidades button { white-space: nowrap; padding: 8px 12px; }
  nav { flex: 1 1 100%; }
  .tab { flex: 1; padding: 10px 4px; }
  .mes { flex: 1; }
  .mes input { flex: 1; }

  main { padding: 16px; }
  .card, .bloco { padding: 14px; }
  .card strong { font-size: 22px; }
  .grafico { height: 320px; }

  /* Lista de lançamentos vem primeiro; formulário e importação depois */
  #lancamentos .grade > .largo { order: -1; }

  /* Tabela de lançamentos vira cartões: descrição + valor / categoria / data + ações */
  .tabela-lanc thead { display: none; }
  .tabela-lanc, .tabela-lanc tbody { display: block; }
  .tabela-lanc tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "desc valor" "cat cat" "data acoes";
    column-gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border);
  }
  .tabela-lanc tr:last-child { border-bottom: 0; }
  .tabela-lanc td { padding: 0; border: 0; background: none; }
  .tabela-lanc td:nth-child(1) { grid-area: data; align-self: center; font-size: 13px; color: var(--muted); }
  .tabela-lanc td:nth-child(2) { grid-area: desc; max-width: none; font-weight: 500; }
  .tabela-lanc td:nth-child(3) { grid-area: cat; margin: 4px 0 0 -8px; min-width: 0; }
  .tabela-lanc td:nth-child(4) { grid-area: valor; font-weight: 600; }
  .tabela-lanc td:nth-child(5) { grid-area: acoes; }
  .tabela-lanc .cel-cat { min-width: 0; }
  .tabela-lanc .cat-btn { min-width: 0; }
  .linha-retirada { background: color-mix(in srgb, var(--muted) 6%, transparent); border-radius: 8px; padding-inline: 8px !important; }

  /* Seletor de categoria abre como painel na parte de baixo da tela */
  .seletor {
    position: fixed; left: 8px !important; right: 8px; top: auto !important; bottom: 8px;
    width: auto; max-height: 65vh;
  }
  .seletor-col { max-height: 65vh; }

  .titulo-lista .botao { width: 100%; text-align: center; }
  .barra button { flex: 1 1 100%; }
  dialog .acoes button { flex: 1 1 100%; }
}

/* Tela de toque: alvos de pelo menos 40px para o dedo */
@media (pointer: coarse) {
  button.icone { min-width: 40px; min-height: 40px; }
  .cat-btn { min-height: 40px; }
  .sel-item { padding: 11px 10px; }
  .tab, .unidades button { min-height: 40px; }
}

/* ---------- Dashboard: resultado da empresa x retiradas ---------- */
.card small { display: block; margin-top: 2px; font-size: 12px; color: var(--muted); }
.card.destaque { border-color: color-mix(in srgb, var(--primary) 40%, var(--border)); }
.retirada { color: #78716c; }
@media (prefers-color-scheme: dark) { .retirada { color: #a8a29e; } }
@media (max-width: 640px) {
  .cards { grid-template-columns: 1fr 1fr; gap: 10px; }
  .cards .card strong { font-size: 18px; }
  .cards .card:last-child:nth-child(odd) { grid-column: 1 / -1; }
}

/* ---------- Revisão da fatura em PDF ---------- */
dialog.dlg-largo { width: min(760px, calc(100% - 32px)); }
.lista-fatura { max-height: 50vh; overflow: auto; margin: 8px 0 16px; border: 1px solid var(--border); border-radius: 8px; }
.lista-fatura thead th { position: sticky; top: 0; background: var(--surface); }
.lista-fatura td { font-size: 13px; }
.lista-fatura tr.ignorado td { color: var(--muted); text-decoration: line-through; }
.lista-fatura tr.ignorado small { text-decoration: none; display: inline-block; }
.confere { padding: 8px 12px; border-radius: 8px; font-size: 13px; }
.confere.ok { background: color-mix(in srgb, var(--pos) 12%, transparent); color: var(--text); }
.confere.alerta { background: color-mix(in srgb, #f59e0b 18%, transparent); color: var(--text); }
