:root {
  --verde-escuro: #2e5d43;
  --verde: #3f7d5a;
  --verde-claro: #e8f3ec;
  --creme: #fbfaf6;
  --texto: #26302a;
  --texto-suave: #64716a;
  --borda: #dde5df;
  --vermelho: #b3423f;
  --vermelho-claro: #fbeceb;
  --sombra: 0 1px 3px rgba(38, 48, 42, 0.1);
  --raio: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--creme);
  color: var(--texto);
  line-height: 1.45;
}

.topo {
  background: var(--verde-escuro);
  color: #fff;
  padding: 22px 24px 18px;
}
.topo h1 { margin: 0; font-size: 1.5rem; font-weight: 650; }
.subtitulo { margin: 4px 0 0; opacity: 0.85; font-size: 0.95rem; }
.impressao-data { display: none; }

/* Resumo (cartões de totais) */
.resumo {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 18px 24px 6px;
}
.cartao {
  background: #fff;
  border: 1px solid var(--borda);
  border-radius: var(--raio);
  box-shadow: var(--sombra);
  padding: 10px 18px;
  min-width: 130px;
}
.cartao .valor { font-size: 1.5rem; font-weight: 700; color: var(--verde-escuro); }
.cartao .etiqueta { font-size: 0.8rem; color: var(--texto-suave); text-transform: uppercase; letter-spacing: 0.03em; }

/* Barra de ações */
.barra {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  padding: 12px 24px;
}
#pesquisa {
  flex: 1 1 220px;
  max-width: 340px;
  padding: 9px 12px;
  border: 1px solid var(--borda);
  border-radius: var(--raio);
  font-size: 0.95rem;
  background: #fff;
}
#pesquisa:focus { outline: 2px solid var(--verde); outline-offset: 1px; }
.barra-botoes { display: flex; flex-wrap: wrap; gap: 8px; }

.btn {
  padding: 9px 14px;
  border: 1px solid var(--borda);
  border-radius: var(--raio);
  background: #fff;
  color: var(--texto);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--verde-claro); border-color: var(--verde); }
.btn-primario { background: var(--verde); border-color: var(--verde); color: #fff; font-weight: 600; }
.btn-primario:hover { background: var(--verde-escuro); }
.btn-perigo-suave { color: var(--vermelho); }
.btn-perigo-suave:hover { background: var(--vermelho-claro); border-color: var(--vermelho); }
.btn-pequeno { padding: 5px 10px; font-size: 0.8rem; }

/* Tabela */
main { padding: 6px 24px 20px; }
.tabela-envolvente {
  overflow-x: auto;
  background: #fff;
  border: 1px solid var(--borda);
  border-radius: var(--raio);
  box-shadow: var(--sombra);
}
table { width: 100%; border-collapse: collapse; min-width: 720px; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--borda); vertical-align: top; }
thead th {
  background: var(--verde-claro);
  color: var(--verde-escuro);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  position: sticky;
  top: 0;
}
tbody tr:hover { background: #f6faf7; }
tbody tr:last-child td { border-bottom: none; }

.col-num { width: 40px; color: var(--texto-suave); }
.col-qtd { width: 130px; text-align: center; }
.col-total { width: 70px; text-align: center; font-weight: 700; }
.col-acoes { width: 90px; text-align: right; white-space: nowrap; }
td.col-qtd, th.col-qtd, td.col-total, th.col-total { text-align: center; }
td.col-acoes { text-align: right; }

.nome-item { font-weight: 600; }
.usos-item { font-size: 0.85rem; color: var(--texto-suave); margin-top: 2px; white-space: pre-wrap; }
.usos-vazio { font-style: italic; opacity: 0.7; }

/* Contador de stock */
.contador {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.contador .qtd { min-width: 26px; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }
.contador .qtd.zero { color: var(--texto-suave); opacity: 0.55; font-weight: 400; }
.btn-mais, .btn-menos {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--borda);
  background: #fff;
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.btn-mais:hover { background: var(--verde-claro); border-color: var(--verde); }
.btn-menos:hover { background: var(--vermelho-claro); border-color: var(--vermelho); }
.btn-menos:disabled { opacity: 0.3; cursor: not-allowed; }
.btn-menos:disabled:hover { background: #fff; border-color: var(--borda); }

.btn-icone {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 4px 6px;
  border-radius: 6px;
}
.btn-icone:hover { background: var(--verde-claro); }
.btn-icone.apagar:hover { background: var(--vermelho-claro); }

.remover-diluicao {
  border: none;
  background: none;
  cursor: pointer;
  color: var(--vermelho);
  font-size: 0.85rem;
  margin-left: 4px;
}

.vazio { text-align: center; color: var(--texto-suave); padding: 30px 0; }

/* Histórico */
.historico { padding: 0 24px 16px; }
.historico details {
  background: #fff;
  border: 1px solid var(--borda);
  border-radius: var(--raio);
  padding: 10px 16px;
}
.historico summary { cursor: pointer; font-weight: 600; color: var(--verde-escuro); }
.historico-topo { margin: 10px 0 4px; }
#lista-historico { list-style: none; margin: 8px 0 4px; padding: 0; max-height: 260px; overflow-y: auto; }
#lista-historico li {
  padding: 6px 4px;
  border-bottom: 1px dashed var(--borda);
  font-size: 0.85rem;
}
#lista-historico li time { color: var(--texto-suave); margin-right: 8px; font-variant-numeric: tabular-nums; }

.rodape { padding: 10px 24px 26px; color: var(--texto-suave); font-size: 0.8rem; }

/* Modal */
dialog {
  border: none;
  border-radius: var(--raio);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
  padding: 22px 24px;
  width: min(440px, 92vw);
}
dialog::backdrop { background: rgba(38, 48, 42, 0.45); }
dialog h2 { margin: 0 0 14px; font-size: 1.15rem; color: var(--verde-escuro); }
dialog label { display: block; margin-bottom: 12px; font-size: 0.88rem; font-weight: 600; }
dialog input[type="text"], dialog input[type="number"], dialog textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--borda);
  border-radius: 8px;
  font-size: 0.95rem;
  font-family: inherit;
}
dialog input:focus, dialog textarea:focus { outline: 2px solid var(--verde); outline-offset: 1px; }
.opcional { font-weight: 400; color: var(--texto-suave); font-size: 0.8rem; }
#campos-diluicoes { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 12px; }
#campos-diluicoes label { flex: 1 1 90px; margin-bottom: 0; }
.modal-acoes { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.erro { color: var(--vermelho); font-size: 0.85rem; }

/* Gestor de diluições */
thead th[data-diluicao] { cursor: grab; touch-action: none; }
.item-diluicao { touch-action: none; }
.a-arrastar { opacity: 0.45; cursor: grabbing !important; }
.sem-selecao, .sem-selecao * { user-select: none !important; -webkit-user-select: none !important; }
.arrasto-alvo { outline: 2px dashed var(--verde); outline-offset: -2px; background: var(--verde-claro) !important; }
.dica { margin: 0 0 10px; font-size: 0.83rem; color: var(--texto-suave); }
#lista-diluicoes { list-style: none; margin: 0 0 14px; padding: 0; }
.item-diluicao {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 6px;
  border: 1px solid var(--borda);
  border-radius: 8px;
  margin-bottom: 6px;
  background: #fff;
  cursor: grab;
}
.item-diluicao.escondida .nome-diluicao { color: var(--texto-suave); font-style: italic; }
.pega { color: var(--texto-suave); cursor: grab; }
.nome-diluicao { flex: 1; font-weight: 600; font-size: 0.9rem; }
.acoes-diluicao { display: flex; gap: 2px; }
.acoes-diluicao .btn-icone:disabled { opacity: 0.25; cursor: default; }
.linha-adicionar { display: flex; gap: 8px; }
.linha-adicionar input { flex: 1; margin-top: 0 !important; }

/* Impressão / Guardar PDF: só o essencial — cabeçalho, totais e a tabela */
@media print {
  body { background: #fff; }
  .barra, .historico, .rodape, .vazio,
  .col-acoes, .btn-mais, .btn-menos, .remover-diluicao { display: none !important; }
  .topo { background: none; color: var(--texto); padding: 0 0 8px; }
  .subtitulo { display: none; }
  .impressao-data { display: block; margin: 2px 0 0; color: var(--texto-suave); font-size: 0.9rem; }
  .resumo { padding: 8px 0; }
  .cartao { box-shadow: none; padding: 6px 12px; min-width: 100px; }
  .cartao .valor { font-size: 1.1rem; }
  main { padding: 0; }
  .tabela-envolvente { box-shadow: none; border: none; overflow: visible; }
  table { min-width: 0; }
  thead th { position: static; background: none; border-bottom: 2px solid var(--texto); }
  th, td { padding: 5px 8px; font-size: 0.85rem; }
  tbody tr { break-inside: avoid; }
  .contador { gap: 0; }
  .usos-vazio { display: none; }
  .qtd.zero { opacity: 1; }
}

/* Telemóvel */
@media (max-width: 640px) {
  .topo { padding: 16px; }
  .resumo, .barra, main, .historico, .rodape { padding-left: 12px; padding-right: 12px; }
  .cartao { min-width: 100px; padding: 8px 12px; }
  .cartao .valor { font-size: 1.2rem; }
}
