/* Mobile-first: uma coluna, alvos de toque >= 44 px, texto de campos >= 16 px (o iOS não faz zoom). */
:root {
  --fundo: #eef2f4;
  --cartao: #ffffff;
  --texto: #14232b;
  --suave: #5a6b74;
  --linha: #d5dde1;
  --primaria: #173f4f;
  --primaria-texto: #ffffff;
  --realce: #1f6f8b;
  --perigo: #c62828;
  --perigo-fundo: #fdecec;
  --ok: #2e7d32;
  --campo: #f6f8f9;
  --sombra: 0 1px 2px rgba(0, 0, 0, 0.08);
  --raio: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --fundo: #0f1a20;
    --cartao: #172830;
    --texto: #e8eef1;
    --suave: #9bb0ba;
    --linha: #2a404b;
    --primaria: #2a6a83;
    --realce: #6cc0de;
    --perigo: #ff8a80;
    --perigo-fundo: #3a1f1f;
    --ok: #81c784;
    --campo: #102029;
    --sombra: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { margin: 0; overflow-x: hidden; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--fundo);
  color: var(--texto);
  touch-action: manipulation; /* sem atraso nem zoom no duplo toque */
  -webkit-tap-highlight-color: transparent;
}
body.com-folha { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2 { margin: 0; }
[hidden] { display: none !important; }

/* ---------- topo ---------- */
.topo {
  position: sticky; top: 0; z-index: 10;
  background: var(--primaria); color: var(--primaria-texto);
  padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 12px) 8px calc(env(safe-area-inset-left) + 12px);
  display: flex; align-items: center; gap: 8px; min-height: 56px;
}
.topo h1 { font-size: 1.15rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topo a, .topo button {
  color: inherit; text-decoration: none; background: rgba(255, 255, 255, 0.14); border: 0; border-radius: 10px;
  min-height: 44px; min-width: 44px; padding: 0 14px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer;
}
.ponto { width: 12px; height: 12px; border-radius: 50%; background: #66d17a; flex: none; }
body.offline .ponto { background: #ff6b6b; }
.aviso-offline {
  background: var(--perigo); color: #fff; padding: 10px 14px; font-weight: 600; text-align: center;
}
body:not(.offline) .aviso-offline { display: none; }

/* ---------- página ---------- */
.pagina { max-width: 720px; margin: 0 auto; padding: 12px calc(env(safe-area-inset-right) + 12px) calc(env(safe-area-inset-bottom) + 90px) calc(env(safe-area-inset-left) + 12px); }
.filtros { display: grid; gap: 8px; margin-bottom: 10px; }
.filtros .linha { display: flex; gap: 8px; align-items: stretch; }
.filtros .linha > * { min-width: 0; }
input[type="search"], input[type="text"], input[type="password"], select {
  width: 100%; min-height: 48px; padding: 8px 12px; font-size: 16px;
  background: var(--cartao); border: 1px solid var(--linha); border-radius: 12px; outline: none;
}
input:focus, select:focus, button:focus-visible, a:focus-visible { outline: 3px solid var(--realce); outline-offset: 1px; }
.linha select { flex: 1 1 0; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; min-height: 48px; padding: 0 14px; border-radius: 24px;
  background: var(--cartao); border: 1px solid var(--linha); cursor: pointer; user-select: none; font-weight: 600;
}
.chip input { width: 22px; height: 22px; margin: 0; accent-color: var(--perigo); }
.chip:has(input:checked) { border-color: var(--perigo); color: var(--perigo); background: var(--perigo-fundo); }
.icone-btn {
  flex: none; width: 48px; min-height: 48px; border-radius: 12px; background: var(--cartao); border: 1px solid var(--linha); cursor: pointer; font-size: 1.2rem;
}
.resumo { color: var(--suave); display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin: 4px 2px 10px; }
.resumo b { color: var(--perigo); }
.banner {
  display: flex; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 10px;
  background: var(--cartao); border: 1px solid var(--realce); border-radius: 12px; padding: 8px 8px 8px 14px;
}
.banner button { min-height: 44px; padding: 0 16px; border-radius: 10px; border: 0; background: var(--primaria); color: var(--primaria-texto); font-weight: 600; cursor: pointer; }

/* ---------- cartão da lista ---------- */
.lista { display: grid; gap: 10px; }
.cartao {
  background: var(--cartao); border-radius: var(--raio); box-shadow: var(--sombra); border: 1px solid var(--linha);
  border-left: 6px solid var(--linha); padding: 12px; display: grid; gap: 10px; min-width: 0;
}
.cartao.sim { border-left-color: var(--perigo); }
.cartao.recebido { animation: piscar 1.2s ease-out; }
@keyframes piscar { from { background: color-mix(in srgb, var(--realce) 30%, var(--cartao)); } to { background: var(--cartao); } }
.cartao-topo { display: flex; gap: 8px; justify-content: space-between; align-items: baseline; min-width: 0; }
.nome { font-size: 1.05rem; overflow-wrap: anywhere; min-width: 0; }
.cat { flex: none; font-size: 0.8rem; color: var(--suave); background: var(--campo); border-radius: 8px; padding: 2px 8px; max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cartao-meio { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button {
  width: 52px; height: 52px; border-radius: 14px; border: 1px solid var(--linha); background: var(--campo);
  font-size: 1.6rem; line-height: 1; cursor: pointer; display: grid; place-items: center; padding: 0;
}
.stepper button:active { background: var(--linha); }
.stepper input {
  width: 76px; min-height: 52px; text-align: center; font-size: 1.25rem; font-weight: 700; padding: 0 4px;
  background: var(--campo); border: 1px solid var(--linha); border-radius: 14px;
}
.selo { min-width: 76px; text-align: center; padding: 10px 12px; border-radius: 12px; font-weight: 800; letter-spacing: 0.03em; background: var(--campo); color: var(--suave); }
.selo.sim { background: var(--perigo); color: #fff; }
.selo.nao { color: var(--ok); }
.obs { background: var(--campo) !important; }
.conflito { background: var(--perigo-fundo); color: var(--texto); border: 1px solid var(--perigo); border-radius: 12px; padding: 10px; display: grid; gap: 8px; }
.conflito .botoes { display: flex; gap: 8px; flex-wrap: wrap; }
.conflito button { flex: 1 1 140px; min-height: 44px; border-radius: 10px; border: 1px solid var(--perigo); background: var(--cartao); font-weight: 600; cursor: pointer; }
.vazio { text-align: center; color: var(--suave); padding: 36px 12px; }
.vazio a { color: var(--realce); font-weight: 600; }
.mais { display: block; width: 100%; min-height: 52px; margin-top: 12px; border-radius: 12px; border: 1px dashed var(--linha); background: transparent; font-weight: 600; cursor: pointer; }
.carregando { text-align: center; color: var(--suave); padding: 24px; }

/* ---------- produtos ---------- */
.linha-produto { background: var(--cartao); border: 1px solid var(--linha); border-radius: var(--raio); padding: 12px; display: grid; gap: 8px; }
.linha-produto .acoes { display: flex; gap: 8px; align-items: stretch; }
.linha-produto select { flex: 1 1 0; }
.btn { min-height: 48px; min-width: 48px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--linha); background: var(--campo); cursor: pointer; font-weight: 600; }
.btn.perigo { color: var(--perigo); border-color: var(--perigo); }
.btn.primario, button.primario { background: var(--primaria); color: var(--primaria-texto); border-color: var(--primaria); }
.btn:disabled { opacity: 0.55; cursor: default; }
.barra-acoes { display: flex; gap: 8px; margin-bottom: 10px; }
.barra-acoes .btn { flex: 1; }

/* ---------- folhas (diálogos que sobem do fundo) ---------- */
.folha-fundo { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, 0.5); display: flex; align-items: flex-end; justify-content: center; }
.folha {
  width: 100%; max-width: 560px; max-height: 88vh; max-height: 88dvh; background: var(--cartao); border-radius: 20px 20px 0 0;
  display: flex; flex-direction: column; animation: subir 0.2s ease-out;
}
@keyframes subir { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.folha-topo { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.folha-topo h2 { font-size: 1.15rem; }
.icone { width: 44px; height: 44px; border-radius: 22px; border: 0; background: var(--campo); cursor: pointer; font-size: 1.1rem; }
.folha-corpo { overflow: auto; padding: 8px 16px calc(env(safe-area-inset-bottom) + 20px); display: grid; gap: 12px; -webkit-overflow-scrolling: touch; }
.campo { display: grid; gap: 6px; }
.campo label { font-weight: 600; }
.erro { color: var(--perigo); font-weight: 600; }
.linha-cat { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; }
.linha-cat .n { color: var(--suave); font-size: 0.85rem; grid-column: 1 / -1; margin-top: -4px; }

/* ---------- avisos ---------- */
#toasts { position: fixed; z-index: 60; left: 0; right: 0; bottom: calc(env(safe-area-inset-bottom) + 12px); display: grid; gap: 8px; justify-items: center; padding: 0 12px; pointer-events: none; }
.toast { pointer-events: auto; background: #1d2b32; color: #fff; padding: 12px 14px; border-radius: 12px; display: flex; gap: 14px; align-items: center; max-width: 560px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35); }
.toast-acao { background: transparent; border: 0; color: #7fd1ee; font-weight: 800; min-height: 44px; padding: 0 8px; cursor: pointer; }
.aviso-js { padding: 20px; }

/* offline: os controlos de edição ficam bloqueados */
body.offline .cartao button, body.offline .cartao input, body.offline .linha-produto button, body.offline .linha-produto select { pointer-events: none; opacity: 0.55; }
@media (prefers-reduced-motion: reduce) { .cartao.recebido, .folha { animation: none; } }

/* ---------- login ---------- */
.login { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: calc(env(safe-area-inset-top) + 20px) 16px calc(env(safe-area-inset-bottom) + 20px); }
.login-cartao { width: 100%; max-width: 400px; background: var(--cartao); border: 1px solid var(--linha); border-radius: 20px; padding: 24px 20px; display: grid; gap: 14px; box-shadow: var(--sombra); }
.login-cartao img { width: 72px; height: 72px; border-radius: 16px; justify-self: center; box-shadow: 0 0 0 2px var(--linha); }
.login-cartao h1 { text-align: center; font-size: 1.4rem; }
.login-cartao p { margin: 0; text-align: center; color: var(--suave); }
.login-cartao form { display: grid; gap: 12px; }
.login-cartao .campo-senha { display: flex; gap: 8px; }
.login-cartao .campo-senha input { flex: 1; min-width: 0; }
.login-cartao .btn.primario { min-height: 52px; font-size: 1.05rem; }
.login-cartao .erro { min-height: 1.4em; }
.sessao-acoes { display: flex; gap: 8px; flex: 1 1 220px; justify-content: flex-end; }
.sessao-acoes > * { flex: 1 1 0; justify-content: center; text-align: center; }
.sessao a.btn { display: inline-flex; align-items: center; text-decoration: none; color: inherit; }
.sessao { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin: 0 2px 10px; color: var(--suave); }
