:root {
  --bg: #f3f5f8; --card: #fff; --ink: #1c2430; --muted: #6b7686; --line: #dde2e9;
  --brand: #0b6bcb; --brand-ink: #fff; --danger: #c0392b; --ok: #1e8e3e; --wa: #1fa855;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
header.top { background: var(--ink); color: #fff; padding: 12px 20px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
header.top h1 { font-size: 17px; margin: 0; flex: 1; font-weight: 600; min-width: 240px; }
.btn { border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 7px 12px; border-radius: 6px; cursor: pointer; font: inherit; text-decoration: none; display: inline-block; }
.btn:hover { filter: brightness(.96); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.wa { background: var(--wa); color: #fff; border-color: var(--wa); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 3px 8px; font-size: 12px; }
header.top .btn:not(.primary):not(.wa) { border-color: #3a4556; background: #2a3442; color: #fff; }

main { display: grid; grid-template-columns: minmax(380px, 520px) 1fr; gap: 20px; padding: 20px; align-items: start; }
@media (max-width: 1100px) { main { grid-template-columns: 1fr; } }

.panel { display: flex; flex-direction: column; gap: 14px; }
details.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; }
details.card > summary { padding: 12px 14px; font-weight: 600; cursor: pointer; list-style: none; display: flex; justify-content: space-between; }
details.card > summary::after { content: "▾"; color: var(--muted); }
details.card:not([open]) > summary::after { content: "▸"; }
.card-body { padding: 0 14px 14px; display: grid; gap: 10px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
label { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
input, select, textarea { font: inherit; color: var(--ink); padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 35%, transparent); border-color: var(--brand); }
textarea { resize: vertical; min-height: 56px; }
.check { display: flex; align-items: center; gap: 6px; color: var(--ink); font-size: 13px; }
.check input { width: auto; }
.unit, .optcfg { border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: grid; gap: 8px; background: #fafbfc; }
.unit-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; font-size: 13px; gap: 8px; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.hint { font-size: 12px; color: var(--muted); margin: 0; }
.hint.warn { color: #a0522d; }
.extra-row { display: grid; grid-template-columns: 1fr 48px 78px 78px 78px auto; gap: 5px; align-items: end; }
.extra-row input { padding: 6px; }
.pt { width: 100%; border-collapse: collapse; font-size: 13px; }
.pt th { font-size: 11px; color: var(--muted); font-weight: 500; text-align: left; padding: 2px 3px; }
.pt td { padding: 2px 3px; }
.pt td:not(:first-child) { width: 92px; }
.pt input { padding: 4px 6px; }
.sub { font-size: 13px; font-weight: 600; margin-top: 4px; }

/* ---------- Documento ---------- */
.doc-wrap { position: sticky; top: 20px; }
.doc { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 36px 40px; max-width: 900px; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,.05); }
.doc-head { display: flex; justify-content: space-between; gap: 20px; border-bottom: 3px solid var(--brand); padding-bottom: 14px; }
.doc-head .co { display: flex; gap: 14px; align-items: center; }
.doc-head img { max-height: 64px; max-width: 140px; object-fit: contain; }
.doc-head h2 { margin: 0; font-size: 20px; }
.doc-head .co small { display: block; color: var(--muted); font-size: 12px; }
.doc-head .num { text-align: right; white-space: nowrap; }
.doc-head .num b { font-size: 18px; color: var(--brand); display: block; }
.doc h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 0 8px; }
.doc .client { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 20px; font-size: 13px; }

.opts { display: grid; grid-template-columns: repeat(var(--n, 3), 1fr); gap: 12px; margin-top: 14px; }
.opt { border: 1px solid var(--line); border-radius: 10px; padding: 16px 14px 14px; display: flex; flex-direction: column; gap: 6px; position: relative; break-inside: avoid; }
.opt.rec { border: 2px solid var(--brand); }
.opt .badge { position: absolute; top: -10px; left: 14px; background: var(--brand); color: #fff; font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 10px; }
.opt .n { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.opt .t { font-weight: 700; font-size: 15px; }
.opt .d { font-size: 12.5px; color: #4a5565; flex: 1; }
.opt .price { font-size: 24px; font-weight: 800; color: var(--ink); margin-top: 6px; }
.opt .price small { display: block; font-size: 11px; font-weight: 500; color: var(--muted); }
.approve { display: block; background: var(--wa); color: #fff !important; text-decoration: none; text-align: center; padding: 10px; border-radius: 7px; font-weight: 700; font-size: 13.5px; }
.approve:hover { filter: brightness(1.07); }
.approve.off { background: #9aa4b1; }

.tbl-scroll { overflow-x: auto; }
.doc table.cmp { width: 100%; border-collapse: collapse; font-size: 12.5px; min-width: 560px; }
.doc table.cmp th { text-align: left; background: #f0f4f9; padding: 7px 8px; font-weight: 600; border-bottom: 1px solid var(--line); vertical-align: bottom; }
.doc table.cmp td { padding: 6px 8px; border-bottom: 1px solid #eef1f5; vertical-align: top; }
.doc table.cmp .r { text-align: right; white-space: nowrap; }
.doc table.cmp tr.group td { background: #fafbfd; font-weight: 600; }
.doc table.cmp .na { color: var(--muted); font-style: italic; font-size: 11.5px; }
.doc table.cmp tfoot td { border-bottom: none; padding: 4px 8px; }
.doc table.cmp tfoot tr.first td { border-top: 2px solid var(--line); padding-top: 8px; }
.doc table.cmp tfoot tr.grand td { font-weight: 800; font-size: 14px; background: #f0f4f9; padding: 8px; }
.doc table.cmp th.rec, .doc table.cmp td.rec { background: color-mix(in srgb, var(--brand) 6%, transparent); }
.doc .terms { font-size: 12.5px; white-space: pre-wrap; }
.doc .foot { margin-top: 28px; font-size: 12px; color: var(--muted); text-align: center; border-top: 1px solid var(--line); padding-top: 10px; }
.doc .empty { color: var(--muted); font-style: italic; padding: 10px 8px; }

/* ---------- Modo cliente ---------- */
.clientbar { display: none; }
body.client header.top, body.client .panel { display: none; }
body.client main { grid-template-columns: 1fr; padding: 16px; }
body.client .doc-wrap { position: static; }
body.client .clientbar { display: flex; justify-content: center; gap: 10px; padding: 12px 16px 0; }
.loaderr { max-width: 520px; margin: 60px auto; text-align: center; background: #fff; padding: 30px; border-radius: 10px; border: 1px solid var(--line); }

/* ---------- Barra inferior do gerador (só no celular/tablet) ---------- */
.mtabs { display: none; }

@media screen and (max-width: 900px) {
  main { grid-template-columns: minmax(0, 1fr); padding: 12px; gap: 12px; }
  body:not(.client) { padding-bottom: calc(68px + env(safe-area-inset-bottom)); }
  body[data-tab="edit"]:not(.client) .doc-wrap { display: none; }
  body[data-tab="view"]:not(.client) .panel { display: none; }
  .doc-wrap { position: static; }

  header.top { padding: 10px 12px; gap: 6px; }
  header.top h1 { flex-basis: 100%; font-size: 15px; min-width: 0; }
  header.top .btn { padding: 6px 10px; font-size: 13px; white-space: nowrap; }
  header.top #btnLink { display: none; }
  header.top .lg { display: none; }
  body.client .mtabs { display: none; }

  .mtabs {
    display: grid; grid-template-columns: 1fr 1fr 1.1fr; gap: 6px;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
    background: #fff; border-top: 1px solid var(--line); box-shadow: 0 -2px 12px rgba(0,0,0,.06);
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  }
  .mtabs button { font: inherit; font-size: 13.5px; font-weight: 600; border: 1px solid var(--line); background: #fff; color: var(--muted); border-radius: 8px; padding: 10px 4px; cursor: pointer; }
  .mtabs button.on { background: var(--ink); color: #fff; border-color: var(--ink); }
  .mtabs button.wa { background: var(--wa); color: #fff; border-color: var(--wa); }

  /* campos com 16px evitam o zoom automático do iPhone */
  input, select, textarea { font-size: 16px; padding: 9px 10px; }
  .check { font-size: 14px; padding: 3px 0; }
  .check input { width: 18px; height: 18px; }
  details.card > summary { padding: 14px; font-size: 15px; }
  .unit-head, .optcfg .unit-head { flex-wrap: wrap; }

  .extra-row { grid-template-columns: repeat(4, minmax(0, 1fr)) auto; padding-bottom: 8px; border-bottom: 1px dashed var(--line); }
  .extra-row > :first-child { grid-column: 1 / -1; }
  .extra-row.hint { border: none; padding: 0; }
  .extra-row input { padding: 8px 6px; }

  .pt td:not(:first-child) { width: 74px; }
  .pt input { padding: 6px 5px; font-size: 15px; }

  dialog { width: 94vw; }
  dialog .db .btn { flex: 1 1 100%; text-align: center; padding: 11px; }
}

@media screen and (max-width: 680px) {
  .doc { padding: 20px 16px; }
  .opts { grid-template-columns: 1fr; gap: 16px; }
  .opt { padding: 18px 16px 16px; }
  .opt .price { font-size: 28px; }
  .approve { padding: 14px; font-size: 15px; }
  .doc-head { flex-direction: column; gap: 10px; }
  .doc-head .num { text-align: left; }
  .doc .client { grid-template-columns: 1fr; }

  /* Tabela comparativa vira lista: descrição em cima, valores das opções lado a lado embaixo */
  .tbl-scroll { overflow: visible; }
  .doc table.cmp { min-width: 0; font-size: 13px; }
  .doc table.cmp, .doc table.cmp tbody, .doc table.cmp tfoot { display: block; }
  .doc table.cmp thead { display: none; }
  .doc table.cmp tr { display: grid; grid-template-columns: repeat(var(--n, 3), minmax(0, 1fr)); gap: 2px 8px; padding: 9px 0; border-bottom: 1px solid #eef1f5; }
  .doc table.cmp td { display: block; border: none; padding: 0; background: none !important; }
  .doc table.cmp td:first-child { grid-column: 1 / -1; margin-bottom: 3px; }
  .doc table.cmp td.r { text-align: left; white-space: normal; }
  .doc table.cmp td.r::before { content: attr(data-label); display: block; font-size: 10.5px; color: var(--muted); font-style: normal; font-weight: 500; }
  .doc table.cmp td.r.rec::before { color: var(--brand); font-weight: 700; }
  .doc table.cmp tr.group { display: block; background: #f0f4f9; border-radius: 6px; padding: 8px 10px; margin-top: 10px; border: none; }
  .doc table.cmp tfoot tr.first { border-top: 2px solid var(--line); margin-top: 6px; }
  .doc table.cmp tfoot tr td, .doc table.cmp tfoot tr.first td { padding: 0; border: none; }
  .doc table.cmp tfoot tr.grand { background: #f0f4f9; border-radius: 8px; padding: 10px; border: none; margin-top: 6px; }
  .doc table.cmp tfoot tr.grand td { padding: 0; font-size: 15px; background: none; }

  /* Visão do cliente: documento de ponta a ponta, PDF como botão largo */
  body.client main { padding: 0; }
  body.client .doc { border: none; border-radius: 0; box-shadow: none; }
  body.client .clientbar { padding: 10px 16px; background: #fff; border-bottom: 1px solid var(--line); }
  body.client .clientbar .btn { width: 100%; padding: 11px; }
}

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 9px 16px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 10; }
.toast.show { opacity: 1; }

dialog { border: none; border-radius: 10px; padding: 0; width: min(580px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog .dh { padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
dialog .db { padding: 12px 16px 16px; max-height: 65vh; overflow: auto; display: grid; gap: 10px; }
.saved-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid #eef1f5; }
#linkOut { font-family: ui-monospace, Consolas, monospace; font-size: 13px; }

@media print {
  @page { size: A4; margin: 10mm; }
  body { background: #fff; }
  header.top, .panel, .toast, .clientbar, .mtabs { display: none !important; }
  .doc-wrap { display: block !important; }
  main, body.client main { display: block; padding: 0; }
  .doc-wrap { position: static; }
  .doc { border: none; box-shadow: none; padding: 0; max-width: none; }
  .opts { grid-template-columns: repeat(var(--n, 3), 1fr) !important; }
  .doc table.cmp { min-width: 0; }
  .doc table.cmp tr { break-inside: avoid; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
