:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #e2e6ea;
  --text: #1f2937;
  --text-muted: #6b7280;
  --primary: #0f766e;
  --primary-dark: #0b5c56;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --radius: 10px;
  --sidebar-w: 240px;
}

* { box-sizing: border-box; }

/* Garante que o atributo "hidden" sempre esconda o elemento, mesmo quando alguma regra abaixo
   define "display" pra esse mesmo elemento (ex: .field, .topbar-actions, .reports-panel usam
   display:flex — sem isto, essa regra "normal" vence o display:none padrão de [hidden], porque
   têm a mesma especificidade e vêm depois no CSS, e o elemento fica visível mesmo com hidden). */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  background: #16544c;
  background: linear-gradient(180deg, #14554d 0%, #0f3f38 100%);
  color: #e5e7eb;
  padding: 20px 14px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}

.sidebar .brand-mark {
  filter: brightness(0) invert(1);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 24px;
}

.brand-mark {
  height: 36px;
  width: auto;
  object-fit: contain;
}

.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; color: #9ca3af; }

#nav { display: flex; flex-direction: column; gap: 4px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  color: #cbd5e1;
  font-size: 14px;
  transition: background .15s;
}

.nav-item:hover { background: rgba(255,255,255,0.06); }
.nav-item.active { background: var(--primary); color: white; font-weight: 600; }

.nav-group-header {
  margin-top: 10px;
  padding: 8px 12px 2px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.45);
}

.nav-item-sub { padding-left: 24px; font-size: 13.5px; }

.menu-toggle {
  display: none; /* só aparece no breakpoint mobile, lá embaixo */
  background: none;
  border: none;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
  padding: 4px 8px;
  border-radius: 6px;
  flex-shrink: 0;
}
.menu-toggle:hover { background: #eef0f2; }

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,0.45);
  z-index: 90;
}
.sidebar-backdrop[hidden] { display: none; }

.content {
  flex: 1;
  padding: 28px 36px;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 16px;
}

.topbar h1 { font-size: 22px; margin: 0; }

.topbar-actions { display: flex; gap: 10px; }

input[type="search"], input[type="text"], input[type="number"], input[type="date"], input[type="email"], select, textarea {
  font: inherit;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: white;
  color: var(--text);
  outline: none;
}

input[type="search"] { width: 240px; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15,118,110,0.12); }

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, opacity .15s;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: #eef0f2; }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-danger:hover { opacity: 0.85; }

.btn-sm { padding: 6px 12px; font-size: 12.5px; }

.button-field { margin: -4px 0 2px; }

.table-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
}

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }

thead th {
  text-align: left;
  padding: 12px 14px;
  background: #f9fafb;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
  position: sticky;
  top: 0;
}

thead th.sortable {
  cursor: pointer;
  user-select: none;
}

thead th.sortable:hover { background: #f0f2f4; color: var(--text); }

thead th.sorted { color: var(--primary); }

th.filter-cell {
  padding: 6px 8px;
  background: #f9fafb;
  border-bottom: 1px solid var(--border);
  font-weight: 400;
}

th.filter-cell input,
th.filter-cell select {
  width: 100%;
  min-width: 90px;
  padding: 5px 7px;
  font-size: 12.5px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}

th.filter-cell input:focus,
th.filter-cell select:focus {
  outline: none;
  border-color: var(--primary);
}

th.filter-cell.actions-col { background: #f9fafb; }

tbody td {
  padding: 11px 14px;
  border-bottom: 1px solid #f0f2f4;
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}

tbody tr { cursor: pointer; }
tbody tr:hover { background: #f7faf9; }

/* Faturamento: linha inteira em cinza quando a parcela já está paga (fl_pago, via mutesRowWhen) */
tbody tr.row-muted { background: #f9fafb; color: #9aa3ab; }
tbody tr.row-muted:hover { background: #f2f3f5; }
tbody tr.row-muted .badge { opacity: .7; }

/* Faturamento: cabeçalho de grupo (uma linha por Cliente) — clicável, abre/fecha as parcelas */
tbody tr.group-header-row { cursor: pointer; background: #eef2f1; }
tbody tr.group-header-row:hover { background: #e5ebe9; }
tbody tr.group-header-row td {
  padding: 9px 14px;
  font-weight: 700;
  font-size: 13px;
  color: var(--primary-dark);
  white-space: nowrap;
}

/* Faturamento: linha de total, no fim do grid — soma o Valor Final do que está filtrado/buscado */
tbody tr.total-row { cursor: default; background: var(--panel); }
tbody tr.total-row:hover { background: var(--panel); }
tbody tr.total-row td {
  padding: 12px 14px;
  font-size: 13.5px;
  color: var(--text);
  text-align: right;
  border-top: 2px solid var(--border);
  border-bottom: none;
}
tbody tr.total-row strong { color: var(--primary-dark); font-size: 15px; }

.actions-col, .cobranca-actions { white-space: nowrap; width: 1%; }

/* Código é sempre curto (ex: "OS-0001") — não precisa da largura de uma coluna comum */
thead th[data-col="codigo"], tbody td[data-col="codigo"] { width: 1%; white-space: nowrap; }

.icon-btn-sm {
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 15px;
  line-height: 1;
  padding: 5px 7px;
  margin-right: 4px;
  cursor: pointer;
}
.icon-btn-sm:hover { background: #eef0f2; border-color: var(--border); }
.icon-btn-sm:last-child { margin-right: 0; }

.reports-panel { display: flex; flex-direction: column; gap: 12px; }

.report-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.report-card-title { font-size: 15px; font-weight: 600; }
.report-card-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.report-card-actions { display: flex; gap: 8px; flex-shrink: 0; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-ativo { background: #dcfce7; color: #166534; }
.badge-inativo { background: #f3f4f6; color: #4b5563; }
.badge-lead { background: #fef3c7; color: #92400e; }
.badge-cancelado { background: #fee2e2; color: #991b1b; }
.badge-plano { background: #dbeafe; color: #1e40af; }
.badge-sva { background: #ede9fe; color: #5b21b6; }
.badge-neutro { background: #f3f4f6; color: #4b5563; }

/* Checklist de Visitas: legenda de cor por periodicidade (Semanal/Quinzenal/Mensal/Trimestral/Anual). */
.badge-semanal { background: #fce7f3; color: #9d174d; }
.badge-quinzenal { background: #ede9fe; color: #5b21b6; }
.badge-mensal { background: #dcfce7; color: #166534; }
.badge-trimestral { background: #cffafe; color: #155e75; }
.badge-anual { background: #fef9c3; color: #854d0e; }

.empty-state {
  padding: 40px;
  text-align: center;
  color: var(--text-muted);
}

.overlay {
  position: fixed; inset: 0;
  background: rgba(15,23,42,0.35);
  display: flex;
  justify-content: flex-end;
  z-index: 50;
}

.overlay[hidden] { display: none; }

.drawer {
  width: 480px;
  max-width: 100%;
  background: white;
  height: 100%;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 24px rgba(0,0,0,0.12);
  animation: slideIn .18s ease-out;
}

@keyframes slideIn {
  from { transform: translateX(24px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}

.drawer-header h2 { font-size: 17px; margin: 0; }

.icon-btn {
  background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text-muted); line-height: 1;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--text-muted); }
.field .req { color: var(--danger); }
.field-hint-inline { font-weight: 400; font-style: italic; color: var(--text-muted); font-size: 11px; }

.field-locked { opacity: .6; }
.field-locked-note { margin-top: -2px; }

.group-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  margin-top: 6px;
  border-top: 1px dashed var(--border);
  padding-top: 12px;
}

.checkbox-row { display: flex; align-items: center; gap: 8px; flex-direction: row; }
.checkbox-row input { width: 16px; height: 16px; }

.checkbox-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 220px;
  overflow-y: auto;
}
.checkbox-list .checkbox-row { font-size: 13.5px; font-weight: 400; }

/* Campos "itens" (Itens do Serviço) e "sva-itens" (Itens Executados na OS) */
.itens-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 8px;
}
.itens-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
}
.itens-row-edit { padding: 2px 0; flex-wrap: wrap; }
.itens-row-nome { flex: 1; min-width: 0; }
.itens-row-edit .itens-row-nome { padding: 7px 10px; font-size: 13.5px; min-width: 110px; }
.itens-row-valor { width: 110px; flex-shrink: 0; }
.itens-row-qty { width: 70px; flex-shrink: 0; padding: 6px 8px; }
.itens-row-tipo { width: 62px; flex-shrink: 0; padding: 6px 4px; }
.itens-row span.itens-row-nome { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.itens-row-aplica-percentual,
.itens-row-adicional-label {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  white-space: nowrap;
  font-weight: 400;
  color: var(--text-muted);
}
.itens-row-adicional-label { color: var(--text); }

.field-label-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 12.5px; font-weight: 600; color: var(--text-muted); }

.inline-toggle { display: flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 500; color: var(--text-muted); cursor: pointer; white-space: nowrap; }
.inline-toggle input { width: 14px; height: 14px; }

.field input:disabled, .field .derived-input { background: var(--bg); color: var(--text-muted); cursor: not-allowed; }

.drawer-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 22px;
  border-top: 1px solid var(--border);
}

.spacer { flex: 1; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #111827;
  color: white;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13.5px;
  z-index: 100;
  box-shadow: 0 8px 20px rgba(0,0,0,0.2);
}

.toast.error { background: var(--danger); }

.field-error { color: var(--danger); font-size: 12px; margin-top: -8px; }

/* ---------------- anexos: documentos de clientes / fotos de imóveis ---------------- */

.field-hint { margin: 0; font-size: 12.5px; color: var(--text-muted); }

.upload-input { font-size: 13px; }

.file-list { display: flex; flex-direction: column; gap: 6px; }

.file-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
}

.file-item a { flex: 1; font-size: 13px; color: var(--primary); text-decoration: none; word-break: break-all; }
.file-item a:hover { text-decoration: underline; }

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
}

.photo-item {
  position: relative;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg);
}

.photo-item img { width: 100%; height: 100%; object-fit: cover; display: block; }

.file-remove, .photo-remove {
  border: none;
  border-radius: 50%;
  background: var(--danger-bg);
  color: var(--danger);
  width: 22px;
  height: 22px;
  line-height: 1;
  font-size: 15px;
  cursor: pointer;
  flex-shrink: 0;
}

.file-remove:hover, .photo-remove:hover { background: var(--danger); color: #fff; }

.versioned-file-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  margin-bottom: 6px;
}

.versioned-file-item a { flex: 1; font-size: 13px; color: var(--primary); text-decoration: none; word-break: break-all; }
.versioned-file-item a:hover { text-decoration: underline; }

.versioned-file-date { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

.versioned-file-toggle {
  border: none;
  background: none;
  color: var(--primary);
  font-size: 12.5px;
  cursor: pointer;
  padding: 4px 0;
}

.versioned-file-toggle:hover { text-decoration: underline; }

.photo-remove {
  position: absolute;
  top: 4px;
  right: 4px;
}

/* ---------------- sidebar: usuário logado / sair ---------------- */

.sidebar-footer {
  margin-top: auto;
  padding-top: 14px;
  border-top: 1px solid rgba(255,255,255,0.1);
}

.user-badge {
  font-size: 12px;
  color: #9ca3af;
  margin-bottom: 8px;
  line-height: 1.4;
}

.user-badge strong {
  display: block;
  color: #f3f4f6;
  font-size: 13.5px;
  font-weight: 600;
}

.logout-btn {
  width: 100%;
  background: rgba(255,255,255,0.08);
  color: #e5e7eb;
  border: none;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background .15s;
}

.logout-btn:hover { background: rgba(255,255,255,0.16); }

/* ---------------- tela de login ---------------- */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(180deg, #14554d 0%, #0f3f38 100%);
}

.login-card {
  background: white;
  padding: 34px 30px 26px;
  border-radius: var(--radius);
  width: 340px;
  max-width: 90vw;
  box-shadow: 0 24px 60px rgba(0,0,0,0.3);
}

.login-brand { padding: 0 0 24px; justify-content: center; flex-direction: column; }
.login-brand .brand-mark { height: 72px; margin-bottom: 4px; }
.login-brand-name { color: var(--text); }
.login-brand-sub { color: var(--text-muted); }

.login-card .field { margin-bottom: 14px; }
.login-card input { width: 100%; }
.login-submit { width: 100%; margin-top: 6px; }

.login-error {
  margin-top: 14px;
  color: var(--danger);
  font-size: 13px;
  text-align: center;
}

/* ---------------- mobile (celular e tablets pequenos) ---------------- */
/* O menu lateral vira uma gaveta que fica por cima do conteúdo (aberta via botão ☰), em vez de
   ocupar espaço fixo ao lado — numa tela estreita a barra fixa de 240px não sobra espaço útil. */
@media (max-width: 768px) {
  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform .2s ease-out;
    box-shadow: 4px 0 24px rgba(0,0,0,0.25);
    /* Com 4 grupos de menu (CRM, Operações, Cadastros, Biblioteca) o conteúdo passa da altura da
       tela em celulares menores — sem isto, o excesso ficava cortado e não dava pra rolar até ele. */
    overflow-y: auto;
  }

  .sidebar.open { transform: translateX(0); }

  .sidebar-backdrop:not([hidden]) { display: block; }

  /* Enquanto a gaveta do menu está aberta, trava o scroll do conteúdo por trás — senão um gesto
     de rolar sobre o menu "vaza" e rola a tela de fundo em vez do próprio menu. */
  body.sidebar-open-lock { overflow: hidden; }

  .content { padding: 16px; }

  .topbar { flex-wrap: wrap; row-gap: 12px; }
  .topbar h1 { font-size: 18px; }

  /* Busca + botões de ação quebram numa segunda linha, ocupando a largura toda. */
  .topbar-actions { flex-wrap: wrap; width: 100%; }
  .topbar-actions input[type="search"] { width: 100%; }
  .topbar-actions .btn { flex: 1; min-width: 0; white-space: nowrap; }

  table { font-size: 12.5px; }
  thead th, tbody td, th.filter-cell { padding: 8px 10px; }

  .drawer { width: 100%; }

  /* Campos lado a lado (mecanismo "row" em entities.js) empilham em telas estreitas — não cabem
     dois campos numa linha só com o formulário já ocupando a largura toda da tela. */
  .field-row { flex-direction: column; }

  .drawer-footer { flex-wrap: wrap; }

  .report-card { flex-direction: column; align-items: stretch; gap: 10px; }
  .report-card-actions { width: 100%; }
  .report-card-actions .btn { flex: 1; }
}
