:root {
  --primary: #1f4e79;
  --bg: #f3f5f8;
  --card: #fff;
  --text: #1f2933;
  --muted: #6b7785;
  --border: #dde2e8;
  --ok: #1e8e3e;
  --warn: #c77700;
  --err: #c62828;
  --radius: 8px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--primary); }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 16px 0 8px; }

/* Layout */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 230px; background: var(--primary); color: #fff; display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar .brand { padding: 16px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid rgba(255,255,255,.15); min-height: 64px; }
.sidebar .brand img { max-height: 40px; max-width: 120px; background: #fff; border-radius: 4px; padding: 2px; }
.sidebar .brand span { font-weight: 600; }
.sidebar nav { flex: 1; padding: 8px 0; }
.sidebar nav a { display: block; padding: 10px 18px; color: rgba(255,255,255,.88); text-decoration: none; }
.sidebar nav a:hover, .sidebar nav a.active { background: rgba(255,255,255,.14); color: #fff; }
.sidebar .user { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.15); font-size: 12px; }
.sidebar .user button { margin-top: 6px; }
.main { flex: 1; padding: 24px; min-width: 0; }
.topbar-mobile { display: none; }
/* (regras de celular no final do arquivo) */

/* Componentes */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 180px; }
.row > .shrink { flex: 0 0 auto; }
.grid-kpi { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.kpi .sub { color: var(--muted); font-size: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1000px) { .grid-2 { grid-template-columns: 1fr; } }
.chart-box { position: relative; height: 280px; }

label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
textarea { min-height: 80px; resize: vertical; }
.field { margin-bottom: 12px; }
.check { display: flex; align-items: center; gap: 6px; }
.check input { width: auto; }

button, .btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 6px; border: 1px solid var(--border);
  background: #fff; color: var(--text); cursor: pointer; font: inherit; text-decoration: none; white-space: nowrap; }
button:hover, .btn:hover { filter: brightness(.96); }
button:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn-danger { background: var(--err); border-color: var(--err); color: #fff; }
.btn-wa { background: #25a244; border-color: #25a244; color: #fff; }
.btn-sm { padding: 4px 9px; font-size: 12px; }
.btn-link { border: 0; background: none; color: var(--primary); padding: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; }

table { width: 100%; border-collapse: collapse; background: #fff; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: 12px; color: var(--muted); text-transform: uppercase; background: #fafbfc; }
td.num, th.num { text-align: right; white-space: nowrap; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #f7f9fc; }
tr.sub td { background: #fbfbfd; color: var(--muted); font-size: 12.5px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
.empty { padding: 24px; text-align: center; color: var(--muted); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 10px; color: var(--muted); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11.5px; font-weight: 600; background: #e8edf3; color: #3b4a5a; }
.badge.rascunho { background: #eceff1; }
.badge.enviado { background: #e3f2fd; color: #1565c0; }
.badge.validado, .badge.aguardando_aprovacao { background: #fff4e0; color: #a35b00; }
.badge.aprovado { background: #e6f4ea; color: var(--ok); }
.badge.integrado { background: #d7f0e0; color: #11632b; }
.badge.reprovado, .badge.erro_integracao { background: #fdecea; color: var(--err); }
.badge.cancelado, .badge.perdido { background: #f1f1f1; color: #777; }
.badge.legado { background: #ede7f6; color: #5e35b1; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.err-text { color: var(--err); }
.ok-text { color: var(--ok); }
.thumb { width: 44px; height: 44px; object-fit: contain; border: 1px solid var(--border); border-radius: 4px; background: #fff; }
.logo-preview { max-height: 80px; max-width: 240px; border: 1px solid var(--border); padding: 4px; background: #fff; }
.mono { font-family: ui-monospace, Consolas, monospace; word-break: break-all; }
.hidden { display: none !important; }
.totals { margin-left: auto; max-width: 340px; }
.totals div { display: flex; justify-content: space-between; padding: 3px 0; }
.totals .grand { font-size: 18px; font-weight: 700; color: var(--primary); border-top: 1px solid var(--border); padding-top: 6px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; }
.tabs button.active { border-bottom-color: var(--primary); color: var(--primary); font-weight: 600; }
.geo-ok { color: var(--ok); }
.geo-wait { color: var(--warn); }
.history li { margin-bottom: 6px; }

/* Login */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }

/* Modal */
.modal-bg { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: flex; align-items: flex-start; justify-content: center; padding: 40px 12px; z-index: 50; overflow-y: auto; }
.modal { background: #fff; border-radius: var(--radius); width: 100%; max-width: 640px; padding: 20px; }
.modal.wide { max-width: 980px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* Toast */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: #263238; color: #fff; padding: 10px 14px; border-radius: 6px; max-width: 360px; box-shadow: 0 4px 12px rgba(0,0,0,.2); }
.toast.error { background: var(--err); }
.toast.success { background: var(--ok); }

/* Proposta pública */
.proposal { max-width: 900px; margin: 24px auto; padding: 0 12px; }
.proposal header { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; }
.proposal header img { max-height: 70px; max-width: 220px; }
.proposal .item { display: flex; gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.proposal .item img, .proposal .item .noimg { width: 90px; height: 90px; object-fit: contain; flex-shrink: 0; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
.proposal .item .info { flex: 1; }
.proposal .item .price { text-align: right; white-space: nowrap; }
p { white-space: pre-line; }

/* Comissões */
tfoot .total-row td { font-weight: 700; background: #f2f5f9; border-top: 2px solid var(--border); }
.grand-total { display: flex; justify-content: space-between; align-items: center; font-size: 17px; border-left: 4px solid var(--primary); }
.grand-total strong { color: var(--primary); font-size: 20px; }
.print-only { display: none; }

@media print {
  body { background: #fff; }
  .sidebar, .topbar-mobile, .bottom-nav, .drawer-backdrop, .no-print, #toasts { display: none !important; }
  .main { padding: 0; }
  .print-only { display: block; margin-bottom: 8px; }
  .card { border: 0; padding: 0; margin-bottom: 14px; }
  .extract { break-inside: avoid-page; }
  .table-wrap { border: 0; overflow: visible; }
  th, td { padding: 4px 6px; font-size: 11px; }
  .kpi { border: 1px solid #ccc; }
  tr.clickable { cursor: default; }
}

/* =====================================================================
   CELULAR / TABLET (até 860px)
   ===================================================================== */
.topbar-mobile, .bottom-nav, .drawer-backdrop { display: none; }
.icon-btn { border: 0; background: transparent; color: inherit; font-size: 20px; padding: 6px 10px; min-width: 44px; min-height: 44px;
  display: inline-flex; align-items: center; justify-content: center; text-decoration: none; border-radius: 8px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.modal-head h2 { margin: 0; }
.modal-x { color: var(--muted); }

@media (max-width: 860px) {
  /* Evita o zoom automático do iPhone ao focar campos (exige fonte >= 16px) */
  input, select, textarea, button, .btn { font-size: 16px; }
  body { font-size: 15px; }
  h1 { font-size: 20px; }

  .layout { display: block; }
  .main { padding: 12px 12px calc(84px + env(safe-area-inset-bottom)); }

  /* Barra superior fixa */
  .topbar-mobile { display: flex; position: sticky; top: 0; z-index: 30; align-items: center; justify-content: space-between;
    gap: 6px; background: var(--primary); color: #fff; padding: 4px 4px; padding-top: max(4px, env(safe-area-inset-top));
    box-shadow: 0 1px 4px rgba(0,0,0,.15); }
  .topbar-title { flex: 1; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  /* Menu lateral vira gaveta */
  .sidebar { display: flex; position: fixed; top: 0; bottom: 0; left: 0; width: min(82vw, 300px); z-index: 60;
    transform: translateX(-105%); transition: transform .2s ease; overflow-y: auto; padding-bottom: env(safe-area-inset-bottom); }
  .sidebar.open { transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,.3); }
  .sidebar nav a { padding: 14px 20px; font-size: 16px; }
  .drawer-backdrop.open { display: block; position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 55; }

  /* Navegação inferior */
  .bottom-nav { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: #fff;
    border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -2px 8px rgba(0,0,0,.06); }
  .bottom-nav a, .bottom-nav button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 2px 8px;
    min-height: 56px; border: 0; border-radius: 0; background: none; color: var(--muted); text-decoration: none; font-size: 11px; }
  .bottom-nav .ico { font-size: 20px; line-height: 1.2; }
  .bottom-nav a.active { color: var(--primary); font-weight: 600; box-shadow: inset 0 3px 0 var(--primary); }

  /* Cabeçalho das páginas e ações */
  .page-head { gap: 8px; }
  .page-head > .btn-primary, .page-head > a.btn-primary { width: 100%; justify-content: center; }
  .page-head .actions { width: 100%; }
  .page-head .actions > * { flex: 1; justify-content: center; }
  .card.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .card.actions > * { justify-content: center; white-space: normal; text-align: center; }
  button, .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }

  /* Cartões e grades */
  .card { padding: 12px; margin-bottom: 12px; border-radius: 10px; }
  .grid-kpi { grid-template-columns: 1fr 1fr; gap: 8px; }
  .kpi { padding: 10px; }
  .kpi .value { font-size: 18px; }
  .chart-box { height: 220px; }

  /* Formulários: campos curtos dividem a linha; longos ocupam a linha toda */
  .row { gap: 10px; }
  .row > * { flex: 1 1 140px; min-width: 0; }
  .row > .wide { flex-basis: 100%; }
  .row > .small { flex: 1 1 90px; max-width: calc(50% - 5px); }
  .totals { max-width: none; width: 100%; }

  /* Janelas (modais) em tela cheia com botões fixos no rodapé */
  .modal-bg { padding: 0; align-items: stretch; }
  .modal, .modal.wide { max-width: none; min-height: 100%; border-radius: 0; padding: 0; display: flex; flex-direction: column; }
  .modal-head { position: sticky; top: 0; background: #fff; padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
    border-bottom: 1px solid var(--border); margin: 0; z-index: 2; }
  .modal-body { flex: 1; padding: 12px; }
  .modal-foot { position: sticky; bottom: 0; background: #fff; margin: 0; padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border); }
  .modal-foot > * { flex: 1; justify-content: center; }
  body.modal-open { overflow: hidden; }

  /* Tabelas viram cartões: cada linha é um cartão e cada célula mostra o nome da coluna */
  .table-wrap { border: 0; overflow: visible; border-radius: 0; }
  .table-wrap table { background: transparent; }
  .table-wrap thead { display: none; }
  .table-wrap tbody, .table-wrap tfoot { display: block; }
  .table-wrap tr { display: block; background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 6px 12px; margin-bottom: 10px; }
  .table-wrap td { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 6px 0;
    border-bottom: 1px dashed #eef1f4; text-align: right; }
  .table-wrap td:last-child { border-bottom: 0; }
  .table-wrap td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-transform: uppercase;
    text-align: left; flex-shrink: 0; max-width: 45%; padding-top: 2px; }
  .table-wrap td > * { text-align: right; }
  .table-wrap td > .cell-val { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; min-width: 0; }
  .table-wrap td.is-empty { display: none; }
  .table-wrap td:first-child { font-weight: 600; }
  .table-wrap td:empty, .table-wrap td[data-label=""]:empty { display: none; }
  .table-wrap td[data-label=""]::before { content: none; }
  .table-wrap td[data-label=""] { justify-content: flex-start; }
  .table-wrap td[colspan] { justify-content: flex-start; text-align: left; font-weight: 600; }
  .table-wrap td[colspan]::before { content: none; }
  .table-wrap td input, .table-wrap td select { max-width: 60%; }
  .table-wrap tr.clickable:hover td { background: transparent; }
  .table-wrap tr.clickable:active { background: #f2f6fb; }
  .table-wrap tr.sub { margin: -6px 0 8px 14px; border-style: dashed; padding: 4px 10px; font-size: 13px; }
  .table-wrap tr.sub td { padding: 3px 0; }
  .table-wrap tr.sub td[data-label="Tabela"], .table-wrap tr.sub td[data-label="Negociado"] { display: none; }
  .table-wrap tr.sub td { background: transparent; }
  .table-wrap tfoot tr.total-row { background: #f2f5f9; border-color: var(--primary); }
  .table-wrap tfoot .total-row td { background: transparent; border-top: 0; }
  .thumb { width: 56px; height: 56px; }
  .pager { justify-content: space-between; flex-wrap: wrap; }
  .pager button { min-width: 48px; }

  /* Lista de itens no formulário de orçamento */
  .quote-lines td[data-label="Item"] { flex-direction: column; align-items: stretch; text-align: left; }
  .quote-lines td[data-label="Item"]::before { content: none; }
  .quote-lines td[data-label="Item"] input { max-width: none; margin-top: 6px; }
  .quote-lines td[data-label="Item"] > .cell-val { align-items: stretch; text-align: left; }

  .tabs { overflow-x: auto; flex-wrap: nowrap; }
  .tabs button { flex-shrink: 0; }
  #toasts { left: 12px; right: 12px; bottom: calc(76px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }
  .history { padding-left: 18px; }
  .grand-total { flex-direction: column; align-items: flex-start; gap: 4px; }
}

@media (max-width: 380px) {
  .grid-kpi { grid-template-columns: 1fr; }
  .card.actions { grid-template-columns: 1fr; }
}
