:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --espresso: #2d241e;
  --espresso-light: #5c4d43;
  --sage: #6b8e78;
  --sage-dark: #52705d;
  --border: #e5dfd5;
  --danger: #b93829;
  --danger-bg: #fdf1ef;
  --warn-bg: #fff8e6;
}

* { box-sizing: border-box; font-family: system-ui, -apple-system, sans-serif; }
body { margin: 0; background: var(--bg); color: var(--espresso); }
.hidden { display: none !important; }

/* TOPBAR & ABAS */
.topbar {
  background: var(--espresso);
  color: #fff;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}
.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.1rem;
  margin-right: auto;
  white-space: nowrap;
}
.badge-sub { background: var(--sage); font-size: 0.7rem; padding: 2px 6px; border-radius: 4px; }

.nav-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}
.tab-btn {
  background: transparent; border: 1px solid rgba(255,255,255,0.2); color: #fff;
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 0.88rem;
}
.tab-btn.active { background: var(--sage); border-color: var(--sage); font-weight: 600; }

/* ÁREA DO USUÁRIO LOGADO NA TOPBAR (AO LADO ESQUERDO DO BOTÃO SAIR) */
#topbar-user-info {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  flex-shrink: 0;
}
.topbar-user-email {
  font-size: 0.82rem;
  color: rgba(255, 255, 255, 0.85);
}
.badge-role {
  font-size: 0.68rem;
  padding: 2px 7px;
  border-radius: 4px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.badge-role-admin {
  background: var(--sage);
  color: #fff;
}
.badge-role-visitor {
  background: #d4a373;
  color: #2d241e;
}
#btn-logout {
  flex-shrink: 0;
}

/* LAYOUT & CARDS */
.container { max-width: 1560px; margin: 24px auto; padding: 0 24px; }
.tab-content { display: none; }
.tab-content.active { display: block; }
.section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; flex-wrap: wrap; gap: 10px; }
.section-head h2, .section-head h3, .section-head h4 { margin: 0; }
.actions-row { display: flex; gap: 8px; align-items: center; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 18px; }
.form-box { margin-bottom: 20px; }

/* BUTTONS */
.btn-primary {
  background: var(--sage); color: #fff; border: none; padding: 8px 14px;
  border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 0.88rem;
}
.btn-primary:hover { background: var(--sage-dark); }
.btn-outline {
  background: transparent; border: 1px solid var(--border); color: inherit;
  padding: 7px 12px; border-radius: 6px; cursor: pointer; font-size: 0.86rem;
}
.topbar .btn-outline { border-color: rgba(255,255,255,0.3); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; border: none; padding: 6px 9px; border-radius: 5px; cursor: pointer; font-size: 0.8rem; }
.btn-sm { padding: 5px 9px; font-size: 0.8rem; }
.full { width: 100%; }

/* BADGES */
.badge { padding: 2px 7px; border-radius: 4px; font-size: 0.76rem; font-weight: 600; }
.badge-cnpj { background: #e3f0e8; color: #2a593d; }
.badge-cpf { background: #f3e8d8; color: #7a4b16; }
.badge-warn { background: var(--danger); color: #fff; }
.badge-warn-soft { background: #fde4e1; color: var(--danger); border: 1px solid #f5b7b0; font-size: 0.68rem; padding: 1px 6px; }
.badge-ok { background: #e3f0e8; color: #2a593d; }
.badge-pre-venda { background: #fef0d5; color: #8a5300; border: 1px solid #f2cf8d; }
.badge-brinde { background: #e8e2f8; color: #4b2e83; }
.badge-uso-loja { background: #e2ecf8; color: #1e4b7a; }

/* TABLES */
.table-wrap { overflow-x: auto; padding: 0; }
table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: #faf8f5; font-weight: 600; user-select: none; }
th[data-sort] { cursor: pointer; }
th[data-sort]:hover { background: #efeae2; }
tr.low-stock { background: var(--danger-bg); }
tr.canceled td { opacity: 0.55; text-decoration: line-through; }
tr.canceled td:last-child { opacity: 1; text-decoration: none; }
tr.canceled button { text-decoration: none !important; opacity: 1 !important; }
.col-sku { white-space: nowrap; }
.stock-cell { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }

/* MENU SUSPENSO DE EXPORTAÇÃO (CSV / JSON) */
.export-dropdown {
  position: relative;
  display: inline-block;
}
.export-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  min-width: 155px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(45, 36, 30, 0.12);
  padding: 4px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.export-menu button {
  background: transparent;
  border: none;
  text-align: left;
  padding: 8px 12px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--espresso);
}
.export-menu button:hover {
  background: var(--bg);
  font-weight: 600;
}

/* MODO SOMENTE LEITURA (VISITANTE) */
body.readonly-mode #btn-novo-produto,
body.readonly-mode #btn-novo-lote,
body.readonly-mode #btn-nova-venda,
body.readonly-mode #btn-novo-cliente,
body.readonly-mode #btn-novo-gasto,
body.readonly-mode #form-produto-box,
body.readonly-mode #form-lote-box,
body.readonly-mode #form-venda-box,
body.readonly-mode #modal-cadastro-cliente,
body.readonly-mode #form-gasto-box,
body.readonly-mode #form-add-linha,
body.readonly-mode #form-add-categoria,
body.readonly-mode #form-add-canal,
body.readonly-mode #form-add-cat-gasto,
body.readonly-mode #form-fornecedor,
body.readonly-mode #form-config button[type="submit"] {
  display: none !important;
}

/* Remove a coluna inteira de Ações (cabeçalho + células) nas tabelas principais */
body.readonly-mode #tbl-produtos > thead > tr > th:last-child,
body.readonly-mode #tbl-produtos > tbody > tr > td:last-child,
body.readonly-mode #tbl-fornecedores > thead > tr > th:last-child,
body.readonly-mode #tbl-fornecedores > tbody > tr > td:last-child,
body.readonly-mode #tbl-lotes > thead > tr > th:last-child,
body.readonly-mode #tbl-lotes > tbody > tr.lote-master-row > td:last-child,
body.readonly-mode #tbl-vendas > thead > tr > th:last-child,
body.readonly-mode #tbl-vendas > tbody > tr.lote-master-row > td:last-child,
body.readonly-mode #tbl-clientes > thead > tr > th:last-child,
body.readonly-mode #tbl-clientes > tbody > tr.lote-master-row > td:last-child,
body.readonly-mode #tbl-gastos > thead > tr > th:last-child,
body.readonly-mode #tbl-gastos > tbody > tr > td:last-child,
body.readonly-mode #tab-config table > thead > tr > th:last-child,
body.readonly-mode #tab-config table > tbody > tr > td:last-child {
  display: none !important;
}

/* =========================================================
   CAMPOS BLOQUEADOS / SOMENTE LEITURA (CURSOR & VISUAL)
   ========================================================= */
input[readonly],
textarea[readonly],
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed !important;
  background-color: #f2efe9 !important;
  color: var(--espresso-light) !important;
  border-color: var(--border) !important;
  user-select: none;
}

input[readonly]:focus,
textarea[readonly]:focus,
input:disabled:focus,
select:disabled:focus,
textarea:disabled:focus {
  outline: none !important;
  box-shadow: none !important;
  border-color: var(--border) !important;
}