:root {
  --accent: #1f5f8b;
  --accent-dark: #174a6d;
  --accent-soft: #e6f0f7;
  --text: #212529;
  --muted: #6c757d;
  --border: #dde2e6;
  --bg: #f4f6f8;
  --card: #ffffff;
  --danger: #c0392b;
  --whatsapp: #1fa855;
  --radius: 12px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
}

/* ---------- Topo e abas ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--accent);
  color: #fff;
  padding: 10px 16px 0;
  padding-top: max(10px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.15rem; margin-bottom: 8px; }
.brand-logo { height: 30px; width: auto; }
.tabs { display: flex; gap: 4px; }
.tab {
  flex: 1;
  background: none;
  border: 0;
  color: rgba(255, 255, 255, .75);
  font: inherit;
  font-weight: 600;
  padding: 10px 4px;
  border-bottom: 3px solid transparent;
  cursor: pointer;
}
.tab.active { color: #fff; border-bottom-color: #fff; }

main { max-width: 760px; margin: 0 auto; padding: 16px 16px 40px; }
.panel { display: none; }
.panel.active { display: block; }

/* ---------- Cartões e campos ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
h2 { font-size: 1rem; margin: 0 0 12px; }
.muted { color: var(--muted); font-size: .9rem; margin: -6px 0 14px; }
small { color: var(--muted); font-weight: 400; }

.grid2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 600px) { .grid2 { grid-template-columns: 1fr 1fr; } }

.field { display: flex; flex-direction: column; gap: 5px; font-size: .88rem; font-weight: 600; color: #3a4148; }
input[type=text], input[type=tel], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%;
  font: inherit;
  font-size: 16px; /* evita zoom automático no iOS */
  font-weight: 400;
  color: var(--text);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  min-height: 44px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
textarea { resize: vertical; }
.inline { display: flex; gap: 8px; }
.inline select { width: 76px; flex: none; }

.check { display: flex; align-items: center; gap: 10px; margin-top: 12px; font-size: .95rem; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

/* ---------- Seletor Orçamento / Recibo ---------- */
.segmented {
  display: flex;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 14px;
}
.segmented label { flex: 1; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block;
  text-align: center;
  padding: 10px;
  border-radius: 8px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.segmented input:checked + span { background: #fff; color: var(--accent); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

body.is-recibo .only-orc { display: none; }

/* ---------- Itens ---------- */
.item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
  display: grid;
  grid-template-columns: 1fr 1.4fr 40px;
  grid-template-areas:
    "desc desc desc"
    "qty  unit del"
    "total total total";
  gap: 8px;
}
.item .i-desc { grid-area: desc; }
.item .i-qty { grid-area: qty; }
.item .i-unit { grid-area: unit; }
.item .i-del { grid-area: del; }
.item .i-total { grid-area: total; text-align: right; font-size: .9rem; color: var(--muted); }
.item .i-total strong { color: var(--text); }
.item label { display: flex; flex-direction: column; gap: 3px; font-size: .75rem; color: var(--muted); font-weight: 600; }
.i-del {
  align-self: end;
  height: 44px;
  border: 0;
  background: none;
  color: var(--danger);
  font-size: 1.4rem;
  cursor: pointer;
  border-radius: 8px;
}
.i-del:hover { background: #fdecea; }
@media (min-width: 600px) {
  .item {
    grid-template-columns: 1fr 70px 110px 110px 40px;
    grid-template-areas: "desc qty unit total del";
    align-items: end;
  }
  .item .i-total { padding-bottom: 12px; }
}

/* ---------- Totais ---------- */
.totals { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 10px; }
.totals > div { display: flex; justify-content: space-between; padding: 4px 0; }
.totals .grand { font-size: 1.25rem; color: var(--accent); margin-top: 4px; }

/* ---------- Botões ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 46px;
  padding: 10px 18px;
  border-radius: 10px;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-dark); }
.btn.ghost { background: #fff; color: var(--accent); border-color: var(--border); }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.whatsapp { background: var(--whatsapp); color: #fff; }
.btn.link { background: none; color: var(--danger); padding: 6px 0; min-height: 0; }
.btn.full { width: 100%; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .85rem; }
.btn.danger { color: var(--danger); }

.actions { display: flex; gap: 10px; }
.actions .btn { flex: 1; }
.actions.sticky {
  position: sticky;
  bottom: 0;
  background: var(--bg);
  padding: 10px 0;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  margin-bottom: 14px;
}
.actions.sticky .btn.primary { flex: 2; }

/* ---------- Resultado ---------- */
.result { border-color: var(--accent); }
.result h2 { margin-bottom: 4px; }
.result .muted { margin: 0 0 12px; }
.pixbox { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); display: grid; gap: 10px; justify-items: center; }
.pixbox .field { width: 100%; }
.pixbox textarea { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
#pixCanvas { width: 200px; height: 200px; image-rendering: pixelated; }

/* ---------- Avisos ---------- */
.alert, .info { border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; font-size: .92rem; }
.alert { background: #fff4e5; border: 1px solid #ffd8a8; }
.info { background: var(--accent-soft); border: 1px solid #c5dbea; display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.alert a { color: var(--accent); font-weight: 600; }
.error { color: var(--danger); font-size: .88rem; margin: 8px 0 0; }
.field-error { color: var(--danger); font-size: .82rem; font-weight: 400; margin: 0; }
input.invalid, input.invalid:focus { border-color: var(--danger); outline-color: var(--danger); }
.empty { text-align: center; color: var(--muted); padding: 30px 0; }
.backup-info { font-size: .9rem; margin: 0 0 12px; }
.backup-info.warn { color: #9a5b00; }
.legal-link { text-align: center; font-size: .85rem; margin: 6px 0 20px; }
.legal-link a { color: var(--muted); }
.empty-orca { width: 160px; height: auto; opacity: .9; }
.empty p { margin: 8px 0 0; }

/* ---------- Histórico ---------- */
.hist {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
}
.hist-top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.hist-client { font-weight: 600; word-break: break-word; }
.hist-meta { font-size: .85rem; color: var(--muted); margin-top: 2px; }
.hist-total { font-weight: 700; white-space: nowrap; }
.badge {
  display: inline-block;
  font-size: .72rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 20px;
  margin-right: 6px;
  vertical-align: 1px;
}
.badge.orcamento { background: var(--accent-soft); color: var(--accent); }
.badge.recibo { background: #e3f6ea; color: #157a3c; }
.hist-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }

/* ---------- Perfil ---------- */
.logo-row { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; }
.logo-preview {
  width: 96px;
  height: 64px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .78rem;
  color: var(--muted);
  overflow: hidden;
  flex: none;
  background: #fff;
}
.logo-preview img { max-width: 100%; max-height: 100%; }
.logo-actions { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
#profileForm h2:not(:first-child) { margin-top: 20px; }
#profileForm .btn.full { margin-top: 18px; }

/* ---------- Instalação (PWA) ---------- */
.install-card { display: flex; gap: 14px; align-items: flex-start; border-color: var(--accent); }
.install-card img { flex: none; border-radius: 12px; }
.install-card h2 { margin-bottom: 4px; }
.install-card .muted { margin: 0 0 10px; }

/* ---------- Termos e privacidade ---------- */
.topbar-simple { padding-bottom: 2px; }
.legal h1 { font-size: 1.3rem; margin: 0 0 4px; }
.legal h2 { font-size: 1rem; margin: 20px 0 6px; }
.legal p, .legal li { line-height: 1.55; }
.legal ul { padding-left: 20px; margin: 6px 0; }
.legal .muted { margin: 0 0 8px; }
.legal .highlight { background: var(--accent-soft); border-radius: 8px; padding: 10px 12px; }
.back-link { color: var(--accent); font-weight: 600; text-decoration: none; }
.todo { background: #fff4e5; color: #9a5b00; padding: 0 4px; border-radius: 4px; }

/* ---------- Pré-visualização do PDF ---------- */
.preview-open { margin-top: 10px; }
.preview {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  background: #525659;
}
.preview-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--accent);
  color: #fff;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  font-weight: 600;
}
.preview-back {
  background: none;
  border: 0;
  color: #fff;
  font: inherit;
  font-weight: 600;
  padding: 8px 6px;
  cursor: pointer;
}
.preview-pages { flex: 1; overflow: auto; padding: 12px; -webkit-overflow-scrolling: touch; }
.preview-pages canvas {
  display: block;
  margin: 0 auto 12px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .35);
}
.preview-loading { color: #fff; text-align: center; margin-top: 40px; }
.preview-actions {
  display: flex;
  gap: 10px;
  background: #fff;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.preview-actions .btn { flex: 1; }
body.preview-on { overflow: hidden; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%) translateY(20px);
  background: #212529;
  color: #fff;
  padding: 12px 18px;
  border-radius: 10px;
  font-size: .92rem;
  max-width: calc(100% - 32px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50;
}
.preview-on .toast { bottom: 90px; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
