:root {
  --azul: #0b3d5c;
  --azul-claro: #e8f1f7;
  --texto: #1b2733;
  --borda: #d7dee4;
  --sucesso: #1e7e34;
  --sucesso-claro: #e6f4ea;
  --erro: #b3261e;
  --aviso: #8a5300;
  --fundo: #f5f7f9;
  --fundo-cartao: #ffffff;
  --texto-secundario: #666666;
}

/* Tema escuro — tons pouco saturados nos menus de configuração, como no
   tema claro; só o fundo/superfícies e o texto trocam de contraste. */
:root[data-tema="escuro"] {
  --azul: #4a90c2;
  --azul-claro: #1e2c38;
  --texto: #e4e8ec;
  --borda: #38424c;
  --sucesso: #4caf6a;
  --sucesso-claro: #1e2e22;
  --erro: #e0645a;
  --aviso: #c9924a;
  --fundo: #14181c;
  --fundo-cartao: #1c2126;
  --texto-secundario: #9aa4ad;
}

* { box-sizing: border-box; }

body {
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14.5px;
  margin: 0;
  color: var(--texto);
  background: var(--fundo);
}

header.topo {
  background: var(--azul);
  color: white;
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  row-gap: 0.5rem;
  column-gap: 1rem;
}

header.topo a { color: white; text-decoration: none; }
header.topo .topo-direita {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  row-gap: 0.4rem;
  column-gap: 1.1rem;
  min-width: 0;
  margin-left: auto;
}
.botao-hamburger {
  background: none;
  border: none;
  color: white;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}
.botao-hamburger:hover { background: rgba(255, 255, 255, 0.15); }
#alternar-tema {
  font-size: 1rem;
  line-height: 1;
}
.badge {
  display: inline-block;
  background: var(--vermelho, #c0392b);
  color: #fff;
  border-radius: 999px;
  padding: 0 0.4rem;
  font-size: 0.72rem;
  line-height: 1.4;
  vertical-align: top;
}
/* Notificações de facturas em atraso ficam visualmente mais alarmantes
   quanto mais tempo passa sem serem resolvidas — de um aviso amarelo
   discreto até vermelho a pulsar quando a dívida já vai em mais de 30
   dias, para nunca ficarem esquecidas "como mais um ícone qualquer". */
.badge--aviso { background: #e0a800; }
.badge--grave { background: #e8590c; }
.badge--critica { background: #c0392b; animation: pulsar-alerta 1.4s infinite; }
.sino-notificacoes--critica { animation: pulsar-alerta 1.4s infinite; }
@keyframes pulsar-alerta {
  0%, 100% { box-shadow: 0 0 0 0 rgba(192, 57, 43, 0.55); }
  50% { box-shadow: 0 0 0 6px rgba(192, 57, 43, 0); }
}
header.topo .marca { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
header.topo .topo-direita select {
  font-size: 0.85rem;
  padding: 0.15rem 0.3rem;
  max-width: 6.5rem;
}
header.topo .topo-direita details.menu-config { position: relative; }
header.topo .topo-direita details.menu-config summary {
  list-style: none;
  cursor: pointer;
  font-size: 0.92rem;
  white-space: nowrap;
  padding: 0.3rem 0.55rem;
  border-radius: 4px;
  transition: background-color 0.15s ease;
}
header.topo .topo-direita details.menu-config summary::-webkit-details-marker { display: none; }
header.topo .topo-direita details.menu-config summary::after { content: " \25BE"; }
header.topo .topo-direita details.menu-config summary:hover,
header.topo .topo-direita details.menu-config[open] summary { background: rgba(255, 255, 255, 0.15); }
header.topo .topo-direita details.menu-config summary.activo { background: rgba(255, 255, 255, 0.24); font-weight: 600; }
header.topo .topo-direita details.menu-config .menu-config-lista {
  position: absolute;
  top: calc(100% + 0.3rem);
  right: 0;
  background: var(--azul);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  min-width: 12rem;
  padding: 0.35rem;
  z-index: 20;
}
header.topo .topo-direita details.menu-config .menu-config-lista a {
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
}
header.topo .topo-direita details.menu-config .menu-config-lista a:hover { background: rgba(255, 255, 255, 0.15); }
header.topo .topo-direita .link-botao {
  font-size: 0.92rem;
  background: none;
  border: none;
  color: white;
  text-decoration: none;
  cursor: pointer;
  padding: 0.3rem 0.55rem;
  border-radius: 4px;
  font-family: inherit;
  white-space: nowrap;
}
header.topo .topo-direita .link-botao:hover { background: rgba(255, 255, 255, 0.15); }

/* Barra lateral flutuante — escondida por omissão (fora do ecrã à
   esquerda), sobreposta ao conteúdo quando aberta (nunca empurra o
   conteúdo principal), com fundo semi-transparente por trás a fechar ao
   clicar fora. Agrupada por área de negócio (Vendas/Finanças/CRM/...),
   só o grupo da página actual começa aberto — reduz scroll sem esconder
   por completo as restantes opções. */
/* Barra lateral fixa (não flutuante) — faz parte do layout normal,
   sempre visível ao lado do conteúdo (nunca sobreposta), só muda quando
   o próprio utilizador clica em minimizar/expandir; esse estado
   persiste entre páginas via cookie (ver sidebar_estado em
   empresa/context_processors.py e o JS no fim de templates/base.html) —
   nunca "aparece e desaparece sozinha". */
.corpo-app {
  display: flex;
  align-items: flex-start;
}
.sidebar {
  position: sticky;
  top: 0;
  flex: 0 0 200px;
  width: 200px;
  max-height: 100vh;
  overflow-y: auto;
  background: var(--fundo-cartao);
  border-right: 1px solid var(--borda);
  padding: 0.6rem 0.5rem;
  transition: width 0.15s ease, padding 0.15s ease;
}
body.sidebar-minimizada .sidebar {
  flex-basis: 2.4rem;
  width: 2.4rem;
  padding: 0.6rem 0.3rem;
  overflow: hidden;
}
body.sidebar-minimizada .sidebar-nav { display: none; }
.botao-colapsar-sidebar {
  display: block;
  background: none;
  border: 1px solid var(--borda);
  border-radius: 4px;
  cursor: pointer;
  color: var(--texto-secundario);
  font-size: 0.95rem;
  line-height: 1;
  padding: 0.15rem 0.5rem;
  margin-bottom: 0.5rem;
}
.botao-colapsar-sidebar:hover { color: var(--azul); border-color: var(--azul); }
body.sidebar-minimizada .botao-colapsar-sidebar { transform: rotate(180deg); }
.sidebar-nav { display: flex; flex-direction: column; gap: 0.15rem; }
.grupo-sidebar summary {
  list-style: none;
  cursor: pointer;
  font-weight: 600;
  padding: 0.4rem 0.5rem;
  border-radius: 4px;
  color: var(--texto-secundario);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.grupo-sidebar summary::-webkit-details-marker { display: none; }
.grupo-sidebar summary::after { content: " \25BE"; float: right; }
.grupo-sidebar[open] summary::after { content: " \25B4"; }
.grupo-sidebar a {
  display: block;
  padding: 0.4rem 0.6rem 0.4rem 0.9rem;
  border-radius: 4px;
  color: var(--texto);
  text-decoration: none;
  font-size: 0.82rem;
}
.grupo-sidebar a:hover { background: var(--azul-claro); }
.grupo-sidebar a.activo { background: var(--azul-claro); color: var(--azul); font-weight: 600; }
.sidebar-extra {
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--borda);
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.sidebar-extra a {
  padding: 0.4rem 0.5rem;
  border-radius: 4px;
  color: var(--texto-secundario);
  text-decoration: none;
  font-size: 0.78rem;
}
.sidebar-extra a:hover { background: var(--azul-claro); color: var(--azul); }

@media (max-width: 640px) {
  header.topo { padding: 0.6rem 1rem; }
  header.topo .topo-direita { column-gap: 0.7rem; }
  header.topo .topo-direita .link-botao { font-size: 0.82rem; }
  header.topo .marca { font-size: 1rem; }
  .sidebar { position: fixed; left: 0; bottom: 0; z-index: 40; box-shadow: 4px 0 16px rgba(0, 0, 0, 0.2); }
}

main { flex: 1 1 auto; min-width: 0; max-width: 1300px; margin: 1.5rem auto; padding: 0 1rem; }

.botao-recuar {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  background: none;
  border: 1px solid var(--borda);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  margin-bottom: 0.85rem;
  font-size: 0.85rem;
  color: var(--texto-secundario);
  cursor: pointer;
}
.botao-recuar:hover { color: var(--azul); border-color: var(--azul); }

.cartao {
  background: var(--fundo-cartao);
  border: 1px solid var(--borda);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 0.85rem;
}

/* Sub-secções compactas dentro de uma página de definições/configuração —
   containers menores para caber mais opções sem ecrãs a parecerem
   sobrecarregados (ver empresa/definicoes.html). */
.seccao {
  background: var(--fundo-cartao);
  border: 1px solid var(--borda);
  border-radius: 8px;
  padding: 0.85rem 1rem;
  margin-bottom: 0.85rem;
}
.seccao h3 {
  margin: 0 0 0.6rem;
  font-size: 0.95rem;
  color: var(--texto-secundario);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.grelha-campos {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.4rem 1rem;
}
.grelha-campos p { margin: 0.35rem 0; }

.grelha-seccoes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 0.85rem;
  align-items: start;
}
.grelha-seccoes .seccao { margin-bottom: 0; }

.seccao--identificacao { border-left: 4px solid var(--azul); }
.seccao--fiscal { border-left: 4px solid var(--aviso); }
.seccao--marca { border-left: 4px solid var(--sucesso); }
.seccao--banco { border-left: 4px solid var(--erro); }

details.seccao > summary {
  cursor: pointer;
  font-size: 0.95rem;
  color: var(--texto-secundario);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.6rem;
}
details.seccao[open] > summary { margin-bottom: 0.6rem; }

.campo-cor {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.campo-cor input[type="color"] {
  width: 2.2rem;
  height: 2.2rem;
  padding: 0;
  border: 1px solid var(--borda);
  border-radius: 5px;
  background: none;
  cursor: pointer;
}
.campo-cor input[type="text"] { flex: 1 1 auto; }

.tabela-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.5rem; border-bottom: 1px solid var(--borda); font-size: 0.92rem; }
th { background: var(--azul-claro); }

/* Campos dentro de células de tabela (linhas de documento/lançamento):
   encolhem para caber na coluna em vez de imporem a largura "natural"
   do input, que em ecrãs estreitos empurrava a tabela para fora do ecrã. */
td input, td select {
  width: 100%;
  min-width: 3.5rem;
  box-sizing: border-box;
}

.botao {
  display: inline-block;
  background: var(--azul);
  color: white;
  padding: 0.45rem 0.9rem;
  border: none;
  border-radius: 5px;
  text-decoration: none;
  font-size: 0.9rem;
  cursor: pointer;
}
.botao.secundario { background: white; color: var(--azul); border: 1px solid var(--azul); }
.botao.perigo { background: var(--erro); }

.etiqueta {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 10px;
  font-size: 0.78rem;
  font-weight: 600;
}
.etiqueta.rascunho { background: #f0e6c8; color: var(--aviso); }
.etiqueta.emitido, .etiqueta.confirmado, .etiqueta.confirmada { background: #d9f0dc; color: var(--sucesso); }
.etiqueta.anulado, .etiqueta.anulada { background: #f4d7d4; color: var(--erro); }

.mensagens { list-style: none; padding: 0; margin: 0 0 1rem; }
.mensagens li { padding: 0.6rem 0.9rem; border-radius: 6px; margin-bottom: 0.4rem; font-size: 0.9rem; }
.mensagens li.success { background: #d9f0dc; color: var(--sucesso); }
.mensagens li.error { background: #f4d7d4; color: var(--erro); }
.mensagens li.warning { background: #fbe8c6; color: #7a5200; }

form p { margin: 0.6rem 0; display: flex; flex-direction: column; gap: 0.25rem; }

/* Formulários simples de criar/editar (ver "formulario-grelha" nos
   templates) — campos lado a lado em vez de empilhados numa coluna só,
   para aproveitar a largura do ecrã e evitar rolagem vertical longa.
   Textareas e os botões finais continuam a ocupar a linha toda. */
.formulario-grelha { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0.2rem 1.5rem; align-items: start; }
.formulario-grelha p:has(textarea) { grid-column: 1 / -1; }
.formulario-grelha > button, .formulario-grelha > a.botao, .formulario-grelha > a.botao.secundario {
  grid-column: 1 / -1;
  margin-top: 0.5rem;
  justify-self: start;
}
input:not([type="checkbox"]):not([type="radio"]), select, textarea {
  padding: 0.45rem;
  border: 1px solid var(--borda);
  border-radius: 5px;
  font-size: 0.92rem;
  background: var(--fundo-cartao);
  color: var(--texto);
}
input[type="checkbox"], input[type="radio"] {
  width: 1.1rem;
  height: 1.1rem;
  flex: 0 0 auto;
  align-self: flex-start;
}

/* O ícone nativo do calendário (type="date") é desenhado pelo browser,
   não segue "color"/"background" do input — sem isto fica escuro e
   quase invisível sobre o fundo escuro do tema escuro. */
input[type="date"]::-webkit-calendar-picker-indicator {
  padding: 0.2rem;
  cursor: pointer;
}
:root:not([data-tema="escuro"]) input[type="date"]::-webkit-calendar-picker-indicator {
  opacity: 0.75;
}
:root[data-tema="escuro"] input[type="date"]::-webkit-calendar-picker-indicator {
  filter: invert(1);
}

.totais { text-align: right; font-size: 0.95rem; }
.totais .total { font-size: 1.15rem; font-weight: 700; color: var(--azul); }

.grelha-resumo { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0.75rem; margin-bottom: 1rem; }
.grelha-resumo .cartao { margin-bottom: 0; }
.grelha-resumo .valor { font-size: 1.4rem; font-weight: 700; color: var(--azul); }

/* Vários blocos/tabelas curtas lado a lado (relatórios com várias secções,
   páginas de resumo com várias listas) em vez de empilhados numa coluna só —
   aproveita a largura do ecrã e evita rolagem vertical longa. */
.grelha-relatorio { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 0.5rem 2rem; align-items: start; }
.grelha-relatorio > .cartao { margin-bottom: 0; }

.mestre-detalhe {
  display: flex;
  gap: 1.25rem;
  align-items: flex-start;
  flex-wrap: wrap;
}
.grelha-fichas {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  grid-auto-rows: 1fr;
  gap: 0.6rem;
  flex: 0 0 260px;
  max-width: 100%;
}
.ficha {
  cursor: pointer;
  border: 1px solid var(--borda);
  border-radius: 10px;
  padding: 0.75rem 0.5rem;
  text-align: center;
  font-size: 0.85rem;
  background: var(--fundo-cartao);
  transition: border-color 0.15s, background 0.15s;
  min-width: 0;
  overflow-wrap: break-word;
  word-break: break-word;
  hyphens: auto;
}
.ficha:hover { border-color: var(--azul); }
.ficha.seleccionada { border-color: var(--azul); background: var(--azul-claro); font-weight: 600; }
.ficha.indisponivel { opacity: 0.55; cursor: not-allowed; }
.ficha .ficha-estado {
  display: block;
  margin-top: 0.3rem;
  font-size: 0.72rem;
  color: var(--texto-secundario);
}
.ficha .ficha-estado.ficha-estado--activo {
  display: inline-block;
  color: var(--sucesso);
  background: var(--sucesso-claro);
  border-radius: 4px;
  padding: 0.1rem 0.45rem;
  font-weight: 600;
}
.painel-detalhe-area { flex: 1 1 320px; min-width: 260px; }

footer.rodape {
  max-width: 1000px;
  margin: 0 auto 1.5rem;
  padding: 0 1rem;
  font-size: 0.82rem;
  color: var(--texto-secundario);
}
footer.rodape a { color: var(--texto-secundario); }
