:root {
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #1f3a5f;
  --primary-soft: #e8eef6;
  --primary-ink: #ffffff;
  --danger: #c0262d;
  --danger-soft: #fdecec;
  --radius: 16px;
  --radius-sm: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 2px 8px rgba(15, 23, 42, .04);
  --gutter: clamp(14px, 4vw, 24px);
  --appbar-h: 60px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --accent: #1f3a5f; /* cor do documento, definida pela empresa */
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
h1, h2, p { margin: 0; }

.ico {
  width: 20px; height: 20px; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- Barra superior ---------- */
.appbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  min-height: calc(var(--appbar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) var(--gutter) 0;
  background: var(--primary); color: var(--primary-ink);
}
.appbar-btn {
  appearance: none; border: 0; background: none; color: inherit; cursor: pointer;
  width: 40px; height: 40px; margin-left: -8px; border-radius: 50%;
  display: grid; place-items: center;
}
.appbar-btn:active { background: rgba(255,255,255,.15); }
.appbar-btn .ico { width: 24px; height: 24px; }
.appbar-title { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.appbar-title img { border-radius: 8px; }
.appbar-title > div { min-width: 0; }
.appbar h1 { font-size: 18px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar p { font-size: 12.5px; opacity: .75; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.top-tabs { display: none; gap: 4px; }
.top-tabs a {
  display: flex; align-items: center; gap: 6px; color: inherit; text-decoration: none;
  padding: 8px 14px; border-radius: 999px; font-size: 14px; font-weight: 600; opacity: .8;
}
.top-tabs a.active { background: rgba(255,255,255,.16); opacity: 1; }
.top-tabs .ico { width: 18px; height: 18px; }

/* ---------- Navegação inferior ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: 1fr 1fr;
  height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: rgba(255,255,255,.96); backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 600;
}
.tabbar a .ico { width: 24px; height: 24px; padding: 2px 16px; box-sizing: content-box; border-radius: 999px; }
.tabbar a.active { color: var(--primary); }
.tabbar a.active .ico { background: var(--primary-soft); }

body.has-tabbar .view { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 96px); }
body.has-actionbar .view { padding-bottom: calc(96px + var(--safe-b)); }

/* ---------- Views ---------- */
.view {
  width: 100%; max-width: 760px; margin: 0 auto;
  padding: var(--gutter) var(--gutter) 32px;
}
.hint { color: var(--muted); font-size: 13px; }

/* ---------- Botões ---------- */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 12px;
  min-height: 48px; padding: 0 18px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 600; font-size: 15px; line-height: 1;
  cursor: pointer; background: var(--surface); color: var(--text); text-decoration: none;
  transition: background .15s, transform .1s;
  white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.ghost { background: var(--surface); border-color: var(--border); }
.btn.text { background: none; min-height: 40px; padding: 0 10px; }
.btn.danger { color: var(--danger); }
.btn.ghost.danger { border-color: #f3c7c9; }
.btn.small { min-height: 38px; padding: 0 14px; font-size: 14px; }
.btn.dashed {
  width: 100%; background: var(--surface-2); color: var(--primary);
  border: 1.5px dashed #b8c4d4;
}
.btn.grow { flex: 1; }
.btn.on-dark { background: rgba(255,255,255,.1); color: #fff; }

/* ---------- Avisos ---------- */
.callout {
  display: flex; gap: 12px; align-items: flex-start;
  background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412;
  padding: 14px; border-radius: var(--radius); margin-bottom: 14px;
}
.callout .ico { width: 24px; height: 24px; margin-top: 2px; }
.callout p { font-size: 14px; margin: 2px 0 10px; }
.callout .btn { background: #9a3412; color: #fff; }

/* ---------- Lista ---------- */
.summary {
  display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; margin-bottom: 14px;
}
.summary > div {
  background: var(--surface); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow);
  display: grid; gap: 2px; min-width: 0;
}
.summary span { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.summary b { font-size: clamp(17px, 5vw, 20px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.search {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 0 14px; margin-bottom: 14px; color: var(--muted);
}
.search input { border: 0; padding: 0; min-height: 46px; background: none; outline: none; }
.search:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }

.cards { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  display: grid; grid-template-columns: 44px minmax(0, 1fr) 44px; gap: 12px; align-items: center;
  padding: 14px 14px 12px; position: relative;
}
.card .avatar {
  width: 44px; height: 44px; border-radius: 12px;
  background: var(--primary-soft); color: var(--primary);
  display: grid; place-items: center; font-weight: 800; font-size: 15px;
}
.card .c-main { min-width: 0; color: inherit; text-decoration: none; }
.card .c-main::after { content: ""; position: absolute; inset: 0; border-radius: var(--radius); }
.card .c-title { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .c-service { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.card .c-pdf {
  position: relative; z-index: 1;
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--border);
  display: grid; place-items: center; color: var(--primary); background: var(--surface);
}
.card .c-foot {
  grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding-top: 10px; border-top: 1px solid var(--border);
  font-size: 13px; color: var(--muted); white-space: nowrap;
}
.card .c-total { color: var(--text); font-weight: 800; font-size: 16px; }
.card:active { background: var(--surface-2); }

.empty { color: var(--muted); text-align: center; padding: 48px 16px; }
.empty.small { padding: 24px; }
.empty h2 { color: var(--text); font-size: 18px; margin: 14px 0 4px; }
.empty-ico {
  width: 72px; height: 72px; margin: 0 auto; border-radius: 22px;
  background: var(--primary-soft); color: var(--primary); display: grid; place-items: center;
}
.empty-ico .ico { width: 34px; height: 34px; }

.fab {
  position: fixed; z-index: 15;
  right: var(--gutter); bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
  display: inline-flex; align-items: center; gap: 8px;
  height: 56px; padding: 0 22px 0 18px; border: 0; border-radius: 18px;
  background: var(--primary); color: #fff; font: inherit; font-weight: 700; font-size: 15px;
  box-shadow: 0 6px 18px rgba(31, 58, 95, .35); cursor: pointer;
}
.fab .ico { width: 24px; height: 24px; }
.fab:active { transform: scale(.97); }

/* ---------- Formulários ---------- */
.form { display: grid; gap: 14px; }
.panel {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; display: grid; gap: 14px; min-width: 0;
}
.panel-head { display: flex; align-items: center; gap: 10px; }
.panel-head h2 { font-size: 16px; font-weight: 700; flex: 1; }
.step {
  width: 26px; height: 26px; border-radius: 8px; flex-shrink: 0;
  background: var(--primary-soft); color: var(--primary);
  display: grid; place-items: center; font-size: 13px; font-weight: 800;
}
.badge {
  min-width: 26px; height: 24px; padding: 0 8px; border-radius: 999px;
  background: var(--bg); color: var(--muted); font-size: 12.5px; font-weight: 700;
  display: grid; place-items: center;
}

.field { display: grid; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 600; min-width: 0; }
input, textarea, select {
  font: inherit; font-size: 16px; color: var(--text); font-weight: 400;
  border: 1px solid var(--border); border-radius: 12px;
  min-height: 48px; padding: 12px 14px;
  background: var(--surface-2); width: 100%; min-width: 0;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
input[type="date"] { appearance: none; -webkit-appearance: none; }
input:focus, textarea:focus {
  outline: none; background: var(--surface);
  border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft);
}
input::placeholder, textarea::placeholder { color: #a0aec0; }
textarea { resize: vertical; min-height: 96px; line-height: 1.45; }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); gap: 14px; }

/* Logo e cores */
.logo-panel { justify-items: center; text-align: center; }
.logo-preview {
  width: min(220px, 70vw); aspect-ratio: 16 / 10; border-radius: var(--radius-sm);
  border: 1.5px dashed #cbd5e1; background: var(--surface-2);
  display: grid; place-items: center; overflow: hidden; color: var(--muted);
}
.logo-preview img { max-width: 88%; max-height: 82%; object-fit: contain; }
.logo-preview .ico { width: 36px; height: 36px; opacity: .6; }
.logo-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: center; }

.swatches { display: flex; flex-wrap: wrap; gap: 12px; }
.swatch {
  width: 44px; height: 44px; border-radius: 50%; border: 0; cursor: pointer;
  display: grid; place-items: center; color: #fff;
  box-shadow: inset 0 0 0 2px rgba(0,0,0,.08);
}
.swatch .ico { opacity: 0; width: 22px; height: 22px; stroke-width: 3; }
.swatch.selected { outline: 3px solid var(--surface); box-shadow: 0 0 0 5px currentColor; }
.swatch.selected .ico { opacity: 1; }
.swatch.custom {
  background: conic-gradient(#ef4444, #f59e0b, #22c55e, #3b82f6, #a855f7, #ef4444);
  position: relative; overflow: hidden;
}
.swatch.custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; min-height: 0; padding: 0; }

/* Itens */
#items { display: grid; gap: 10px; }
#items:empty { display: none; }
.item {
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px;
  display: grid; gap: 10px; background: var(--surface-2);
}
.item-top { display: flex; gap: 8px; align-items: center; }
.item-n {
  width: 24px; height: 24px; border-radius: 7px; flex-shrink: 0;
  background: var(--surface); border: 1px solid var(--border);
  display: grid; place-items: center; font-size: 12px; font-weight: 700; color: var(--muted);
}
.item input { background: var(--surface); }
.item-top input { flex: 1; font-weight: 600; }
.item-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, .9fr) minmax(0, 1.5fr); gap: 8px; }
.item-grid .field { font-size: 12px; gap: 4px; }
.item-grid input { padding: 10px; }
.item-sub {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 13px; color: var(--muted); padding-top: 2px;
}
.item-sub b { color: var(--text); font-size: 15px; }
.icon-btn {
  appearance: none; border: 0; background: none; color: var(--muted); cursor: pointer;
  width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; flex-shrink: 0;
}
.icon-btn:hover, .icon-btn:active { background: var(--danger-soft); color: var(--danger); }

.totals {
  display: grid; gap: 8px; padding: 12px 14px; border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border);
}
.totals-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; font-size: 14px; color: var(--muted); }
.totals-row input { width: 140px; min-height: 42px; padding: 8px 12px; text-align: right; background: var(--surface); }
.totals-row.grand { border-top: 1px solid var(--border); padding-top: 10px; color: var(--text); font-size: 16px; font-weight: 600; }
.totals-row.grand strong { font-size: 20px; color: var(--primary); }

.panel-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Barra de ação fixa */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; gap: 12px; align-items: center;
  padding: 10px var(--gutter) calc(10px + var(--safe-b));
  background: rgba(255,255,255,.97); backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(15, 23, 42, .06);
}
.actionbar-total { display: grid; flex: 1; min-width: 0; }
.actionbar-total span { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.actionbar-total strong { font-size: clamp(18px, 5.5vw, 22px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actionbar .btn.primary { min-width: 150px; min-height: 52px; }
/* Na tela com abas, a barra de ação fica logo acima delas */
body.has-tabbar .actionbar { bottom: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: 10px; box-shadow: none; }

/* Toast */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 20px);
  transform: translateX(-50%) translateY(16px);
  max-width: calc(100% - 32px);
  background: #0f172a; color: #fff; padding: 12px 18px; border-radius: 12px; font-size: 14px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Telas maiores ---------- */
@media (min-width: 720px) {
  :root { --appbar-h: 64px; }
  .top-tabs { display: flex; }
  .tabbar { display: none; }
  body.has-tabbar .view { padding-bottom: 120px; }
  body.has-tabbar .actionbar { bottom: 0; padding-bottom: calc(10px + var(--safe-b)); }
  .fab { bottom: 28px; right: max(var(--gutter), calc((100vw - 760px) / 2 + var(--gutter))); }
  #toast { bottom: 100px; }
  .cards { grid-template-columns: 1fr 1fr; }
  .panel { padding: 20px 22px; }
  .actionbar { justify-content: center; }
  .actionbar > * { max-width: 360px; }
  .summary { grid-template-columns: 1fr 1fr; }
}

/* Telas muito estreitas */
@media (max-width: 360px) {
  .item-grid { grid-template-columns: 1fr 1fr; }
  .item-grid .field:last-child { grid-column: 1 / -1; }
  .fab span { display: none; }
  .fab { width: 56px; padding: 0; justify-content: center; }
  .actionbar .btn.primary { min-width: 0; }
}

/* ---------- Preview ---------- */
#doc-wrap { background: #4b5563; min-height: 100vh; min-height: 100dvh; }
.doc-toolbar {
  position: sticky; top: 0; z-index: 10; display: flex; gap: 10px; align-items: center;
  padding: calc(10px + env(safe-area-inset-top, 0px)) var(--gutter) 10px;
  background: #1f2937;
}
.doc-toolbar .btn { min-height: 44px; }
.doc-toolbar-title {
  flex: 1; min-width: 0; color: #e5e7eb; font-size: 14px; font-weight: 600; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
@media (max-width: 420px) {
  .doc-toolbar-title { display: none; }
  .doc-toolbar { justify-content: space-between; }
}
#doc-scaler { padding: 16px 0 calc(24px + var(--safe-b)); overflow: hidden; }
#doc { width: 210mm; margin: 0 auto; }
#doc .page { box-shadow: 0 4px 18px rgba(0,0,0,.35); }

/* ---------- Documento (A4) ---------- */
.page {
  width: 210mm; min-height: 297mm; background: #fff; color: #222;
  padding: 18mm 16mm; margin: 0 auto 16px; position: relative;
  display: flex; flex-direction: column;
  box-shadow: 0 2px 10px rgba(0,0,0,.3);
  font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif; font-size: 10.5pt; line-height: 1.45;
}

/* Capa */
.cover { padding: 0; justify-content: space-between; overflow: hidden; }
.cover-band { height: 14mm; background: var(--accent); }
.cover-body { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20mm; text-align: center; }
.cover-logo { max-width: 110mm; max-height: 70mm; object-fit: contain; margin-bottom: 18mm; }
.cover-logo-fallback { font-size: 28pt; font-weight: 800; color: var(--accent); margin-bottom: 18mm; }
.cover-kicker { letter-spacing: .3em; text-transform: uppercase; font-size: 11pt; color: #666; }
.cover-title { font-size: 26pt; font-weight: 800; color: var(--accent); margin: 4mm 0 2mm; line-height: 1.15; }
.cover-service { font-size: 13pt; color: #444; }
.cover-divider { width: 30mm; height: 1.2mm; background: var(--accent); margin: 12mm auto; border-radius: 1mm; }
.cover-client-label { font-size: 10pt; text-transform: uppercase; letter-spacing: .2em; color: #777; }
.cover-client { font-size: 20pt; font-weight: 700; margin-top: 2mm; }
.cover-foot {
  display: flex; justify-content: space-between; align-items: end; gap: 10mm;
  padding: 0 16mm 10mm; font-size: 9.5pt; color: #555;
}
.cover-foot b { color: #222; }
.cover-foot .right { text-align: right; }

/* Página de conteúdo */
.doc-header {
  display: flex; justify-content: space-between; align-items: center; gap: 8mm;
  padding-bottom: 5mm; border-bottom: 0.8mm solid var(--accent);
}
.doc-header img { max-height: 20mm; max-width: 45mm; object-fit: contain; }
.doc-company { text-align: right; font-size: 9pt; color: #555; line-height: 1.5; }
.doc-company .name { font-size: 13pt; font-weight: 700; color: #222; }

.doc-meta { display: flex; justify-content: space-between; align-items: baseline; margin: 6mm 0 4mm; }
.doc-meta h2 { margin: 0; font-size: 15pt; color: var(--accent); }
.doc-meta .meta-right { font-size: 9.5pt; color: #555; text-align: right; }

.client-box {
  background: #f6f7f9; border-left: 1mm solid var(--accent);
  padding: 3mm 4mm; display: grid; grid-template-columns: 1fr 1fr; gap: 1mm 6mm; font-size: 9.5pt;
}
.client-box .full { grid-column: 1 / -1; }
.client-box span { color: #777; }

.doc-section h3 {
  font-size: 10pt; text-transform: uppercase; letter-spacing: .08em; color: var(--accent);
  margin: 6mm 0 2mm;
}
.doc-section p { margin: 0; white-space: pre-wrap; }

table.items { width: 100%; border-collapse: collapse; font-size: 9.5pt; }
table.items th {
  background: var(--accent); color: #fff; text-align: left; font-weight: 600;
  padding: 2mm 2.5mm; -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
table.items td { padding: 1.8mm 2.5mm; border-bottom: 0.2mm solid #ddd; vertical-align: top; }
table.items tbody tr:nth-child(even) td { background: #fafafa; }
table.items .num { text-align: right; white-space: nowrap; }
table.items .ctr { text-align: center; white-space: nowrap; }
table.items tfoot td { border: none; padding: 1.5mm 2.5mm; }
table.items tfoot .grand td {
  font-size: 12pt; font-weight: 800; color: #fff; background: var(--accent);
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
table.items tr { break-inside: avoid; }
table.items thead { display: table-header-group; }
table.items tfoot { display: table-row-group; } /* total só no final, não repete por página */

.conditions { display: grid; grid-template-columns: 1fr 1fr; gap: 1mm 6mm; font-size: 9.5pt; }
.conditions .full { grid-column: 1 / -1; }
.conditions b { color: #444; }

.signatures {
  margin-top: auto; padding-top: 22mm;
  display: grid; grid-template-columns: 1fr 1fr; gap: 14mm; break-inside: avoid;
}
.sig { text-align: center; font-size: 9.5pt; }
.sig .line { border-top: 0.3mm solid #333; margin-bottom: 1.5mm; }
.sig .who { font-weight: 700; }
.sig .role { color: #666; }
.place-date { text-align: center; font-size: 9.5pt; color: #555; margin-top: 10mm; }

/* Aplicar cores ao imprimir */
.page, .page * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

/* ---------- Impressão ---------- */
@page { size: A4; margin: 0; }
@media print {
  html, body { background: #fff; }
  .no-print, .topbar, #app, #toast { display: none !important; }
  #doc-wrap { display: block !important; background: none; padding: 0; min-height: 0; }
  #doc-scaler { padding: 0; }
  #doc { zoom: 1 !important; width: auto; }
  .page, #doc .page { margin: 0; box-shadow: none; break-after: page; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
  .page:last-child { break-after: auto; }
}
