* { box-sizing: border-box; margin: 0; padding: 0; }

/* Evita flash do conteudo da pagina (#view) antes do shell (sidebar/topbar)
   ser montado por layout.js. Oculto ate receber .shell-ready no body. */
body:not(.shell-ready) #view { visibility: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f7fa;
  color: #1a1a2e;
}

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

/* ---------- Sidebar ---------- */
.sidebar {
  width: 240px;
  background: #16213e;
  color: #e5e7eb;
  display: flex;
  flex-direction: column;
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 100;
  transition: transform .2s ease;
}

.sidebar-header {
  padding: 20px 18px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  display: flex;
  align-items: center;
  gap: 10px;
}
.sidebar-header .logo {
  width: 32px; height: 32px;
  border-radius: 8px;
  background: #4f46e5;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff;
}
.sidebar-header .brand { font-size: 1rem; font-weight: 600; color: #fff; }
.sidebar-header .brand small { display: block; font-size: .7rem; font-weight: 400; color: #9ca3af; }

.sidebar-user {
  padding: 12px 18px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  font-size: .8rem;
  color: #cbd5e1;
}
.sidebar-user strong { color: #fff; display: block; font-size: .9rem; }
.sidebar-user .role { color: #9ca3af; font-size: .75rem; }

.nav { flex: 1; padding: 8px 0; overflow-y: auto; }
.nav-group { margin-bottom: 4px; }
.nav-group-title {
  padding: 10px 18px 4px;
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: #6b7280;
}

/* Pai recolhivel */
.nav-parent {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 18px;
  background: transparent;
  border: none;
  border-left: 3px solid transparent;
  color: #d1d5db;
  font-size: .9rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.nav-parent:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav-parent .caret {
  display: inline-block;
  width: 10px;
  font-size: .6rem;
  color: #9ca3af;
  transition: transform .18s ease;
}
.nav-parent .icon {
  width: 18px; text-align: center; font-size: .95rem; color: #9ca3af;
}
.nav-parent .label { flex: 1; }

.nav-parent-link { text-decoration: none; }
.nav-parent-link:hover { text-decoration: none; }

/* Seta rotaciona quando o grupo esta aberto */
.nav-group.open > .nav-parent .caret { transform: rotate(90deg); color: #a5b4fc; }
.nav-group.open > .nav-parent { color: #fff; }

/* Filhos: ocultos por default, exibidos quando .open */
.nav-children {
  max-height: 0;
  overflow: hidden;
  transition: max-height .22s ease;
}
.nav-group.open > .nav-children { max-height: 600px; }

.nav-subitem {
  display: block;
  padding: 8px 18px 8px 42px;
  color: #cbd5e1;
  text-decoration: none;
  font-size: .85rem;
  border-left: 3px solid transparent;
  transition: background .15s, color .15s;
}
.nav-subitem:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav-subitem.active { background: rgba(79,70,229,.18); color: #fff; border-left-color: #4f46e5; }

/* Terceiro nivel: subgrupo expansivel dentro de um grupo (ex.: Cadastros Financeiros) */
.nav-subgroup { margin: 2px 0; }

.nav-subparent {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 18px 7px 28px;
  background: transparent;
  border: none;
  border-left: 3px solid transparent;
  color: #cbd5e1;
  font-size: .82rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.nav-subparent:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav-subparent .caret {
  display: inline-block;
  width: 10px;
  font-size: .6rem;
  color: #9ca3af;
  transition: transform .18s ease;
}
.nav-subparent .icon {
  width: 18px; text-align: center; font-size: .9rem; color: #9ca3af;
}
.nav-subparent .label { flex: 1; }

.nav-subgroup.open > .nav-subparent .caret { transform: rotate(90deg); color: #a5b4fc; }
.nav-subgroup.open > .nav-subparent { color: #fff; }

.nav-subchildren {
  max-height: 0;
  overflow: hidden;
  transition: max-height .22s ease;
}
.nav-subgroup.open > .nav-subchildren { max-height: 400px; }

.nav-subsubitem {
  display: block;
  padding: 7px 18px 7px 54px;
  color: #cbd5e1;
  text-decoration: none;
  font-size: .8rem;
  border-left: 3px solid transparent;
  transition: background .15s, color .15s;
}
.nav-subsubitem:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav-subsubitem.active { background: rgba(79,70,229,.18); color: #fff; border-left-color: #4f46e5; }

.nav-sep {
  border: 0;
  border-top: 1px solid rgba(255,255,255,.10);
  margin: 6px 14px 6px 24px;
}

.sidebar-footer {
  padding: 12px 18px;
  border-top: 1px solid rgba(255,255,255,.08);
}
.sidebar-footer .btn-logout {
  width: 100%;
  padding: 8px;
  background: transparent;
  border: 1px solid rgba(255,255,255,.15);
  color: #cbd5e1;
  border-radius: 6px;
  font-size: .85rem;
  cursor: pointer;
  transition: background .15s;
}
.sidebar-footer .btn-logout:hover { background: rgba(255,255,255,.05); color: #fff; }

/* ---------- Main area ---------- */
.main {
  flex: 1;
  margin-left: 240px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.topbar {
  height: 56px;
  background: #fff;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  align-items: center;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar .menu-toggle {
  display: none;
  background: transparent;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: #1a1a2e;
  margin-right: 12px;
}
.topbar h1 { font-size: 1.1rem; color: #16213e; }
.topbar .spacer { flex: 1; }

.content { padding: 24px; flex: 1; }

/* ---------- Cards / shared ---------- */
.card {
  background: #fff;
  border-radius: 10px;
  padding: 24px;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
  margin-bottom: 20px;
}
.card h2 { font-size: 1rem; margin-bottom: 16px; color: #16213e; }

.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: .85rem;
  font-weight: 600;
  margin-bottom: 4px;
  color: #2d3748;
}
.checkbox-inline {
  display: inline-flex !important;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-weight: 600;
  margin-bottom: 0 !important;
}
.checkbox-inline input[type="checkbox"] {
  width: auto;
  margin: 0;
  cursor: pointer;
}
.checkbox-inline span { line-height: 1; }
.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: .9rem;
  outline: none;
  transition: border-color .2s;
}
.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus { border-color: #4f46e5; }
.form-group textarea { resize: vertical; min-height: 60px; }

/* Selects: protecao contra texto cortado (so funciona se houver largura
   suficiente; o title em cada <option> e aplicado via JS em layout.js).
   text-overflow: ellipsis e ignorado por alguns browsers em <select>, mas
   mantido por consistencia com o padrao visual dos inputs. */
.form-group select {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .2s;
}
.btn:hover { opacity: .85; }
.btn-primary { background: #4f46e5; color: #fff; }
.btn-secondary { background: #6b7280; color: #fff; }
.btn-danger { background: #ef4444; color: #fff; }
.btn-sm { padding: 6px 12px; font-size: .8rem; }
.btn + .btn { margin-left: 6px; }
.actions { margin-top: 14px; display: flex; gap: 8px; }

table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 1px 8px;
  border-bottom: 1px solid #e5e7eb;
  line-height: 1.1;
}
th { font-size: .8rem; color: #6b7280; text-transform: uppercase; letter-spacing: .04em; padding-top: 4px; padding-bottom: 4px; }
td { font-size: .85rem; }
td:last-child { white-space: nowrap; display: flex; flex-direction: row; gap: 6px; align-items: center; }
td .actions { margin-top: 0; display: flex; flex-direction: row; gap: 6px; }
.empty { text-align: center; color: #9ca3af; padding: 32px 0; }
.loading { text-align: center; color: #6b7280; padding: 32px 0; font-style: italic; }
.error-msg { color: #ef4444; font-size: .85rem; margin-top: 6px; }

/* ---------- Dashboard ---------- */
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.kpi {
  background: #fff;
  border-radius: 10px;
  padding: 18px 20px;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.kpi .kpi-label {
  font-size: .8rem; color: #6b7280; text-transform: uppercase; letter-spacing: .05em;
}
.kpi .kpi-value { font-size: 1.8rem; font-weight: 700; color: #16213e; margin-top: 4px; }
.kpi .kpi-hint { font-size: .8rem; color: #9ca3af; margin-top: 2px; }

.welcome-card { text-align: center; padding: 36px 24px; }
.welcome-card h2 { font-size: 1.4rem; margin-bottom: 6px; }
.welcome-card p { color: #6b7280; font-size: .95rem; }

/* ---------- Dashboard: extras ---------- */
.dashboard-section-title {
  font-size: .72rem;
  font-weight: 700;
  color: #9ca3af;
  text-transform: uppercase;
  letter-spacing: .06em;
  margin: 4px 0 10px;
}
.dashboard-grid-2 {
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}
.dashboard-card { padding: 18px; margin-bottom: 0; }
.dashboard-card h2 { font-size: .95rem; }
.dashboard-card .empty { padding: 18px 0; }
.dashboard-card table th,
.dashboard-card table td { font-size: .82rem; }
.kpi.kpi-warn  { border-left: 4px solid #d97706; }
.kpi.kpi-danger { border-left: 4px solid #ef4444; }
.kpi.kpi-success { border-left: 4px solid #16a34a; }
.dashboard-acesso { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.dashboard-card-link {
  display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 18px;
  font-weight: 600; color: #16213e; text-decoration: none;
  margin-bottom: 0;
  transition: transform .12s, box-shadow .12s;
}
.dashboard-card-link:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,.12); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: none; align-items: center; justify-content: center; z-index: 1500;
}
.modal-overlay.active { display: flex; }
.modal {
  background: #fff; border-radius: 12px; padding: 24px;
  width: 100%; max-width: 380px; box-shadow: 0 10px 30px rgba(0,0,0,.25);
  text-align: center; animation: modalIn .18s ease-out;
}
@keyframes modalIn { from { transform: scale(.92); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal-icon {
  width: 52px; height: 52px; margin: 0 auto 14px;
  border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 1.6rem;
}
.modal-icon.info { background: #e0e7ff; color: #4f46e5; }
.modal-icon.warning { background: #fef3c7; color: #d97706; }
.modal-icon.error { background: #fee2e2; color: #ef4444; }
.modal-icon.success { background: #dcfce7; color: #16a34a; }
.modal h3 { font-size: 1.1rem; margin-bottom: 10px; color: #16213e; }
.modal p { font-size: .9rem; color: #4b5563; line-height: 1.5; margin-bottom: 18px; }
.modal-actions { display: flex; justify-content: center; gap: 8px; }
.modal-actions.hidden { display: none; }

/* ---------- Largura de campos (padrao) ----------
   Classes reutilizaveis aplicadas ao .form-group para definir a largura
   proporcional do campo conforme o tamanho esperado do conteudo.
   Use em todos os formularios: xs (codigo, CEP, sigla), sm (telefone,
   unidade, numero), md (marca, cidade, custo), lg (e-mail, condicao),
   xl (nome, razao social, descricao, endereco).                       */
.form-group.campo-xs { width: 90px;  max-width: 100%; flex: 0 0 90px; }
.form-group.campo-sm { width: 150px; max-width: 100%; flex: 0 0 150px; }
.form-group.campo-md { width: 240px; max-width: 100%; flex: 0 0 240px; }
.form-group.campo-lg { width: 360px; max-width: 100%; flex: 0 0 360px; }
.form-group.campo-xl { width: 100%;  max-width: 100%; flex: 1 1 100%; }

/* ---------- Formulario em secoes (modais e telas de cadastro) ----------
   Padrao obrigatorio: agrupar campos em secoes com titulo discreto e linha
   divisoria sutil, e alinhar os campos pela base para que os inputs fiquem
   na mesma altura mesmo quando os labels tiverem tamanhos diferentes.
   Reusavel em Insumos, Grupos, Fornecedores e futuros formularios.        */
.form-secoes { width: 100%; }
.form-secao {
  border-top: 1px solid #e5e7eb;
  padding-top: 14px;
  margin-bottom: 18px;
}
.form-secao:first-child {
  border-top: none;
  padding-top: 0;
}
.form-secao-title {
  font-size: .72rem;
  font-weight: 700;
  color: #9ca3af;
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 12px;
}
.form-secao-fields {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
}
.form-secao-fields .form-group {
  margin-bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
.form-secao-fields .form-group > label:first-child {
  min-height: 1.5em;
  display: block;
}
.form-secao-fields .span-full { flex: 1 1 100%; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #16213e 0%, #1f2a4d 100%);
  padding: 20px;
}
.login-card {
  background: #fff;
  border-radius: 14px;
  padding: 36px 28px;
  width: 100%; max-width: 380px;
  box-shadow: 0 20px 50px rgba(0,0,0,.3);
}
.login-card .logo {
  width: 56px; height: 56px; margin: 0 auto 14px;
  border-radius: 14px;
  background: #4f46e5;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 1.4rem; color: #fff;
}
.login-card h1 { text-align: center; font-size: 1.3rem; color: #16213e; margin-bottom: 4px; }
.login-card p.subtitle { text-align: center; color: #6b7280; font-size: .85rem; margin-bottom: 22px; }

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .topbar .menu-toggle { display: inline-block; }
  .form-group.campo-xs,
  .form-group.campo-sm,
  .form-group.campo-md,
  .form-group.campo-lg,
  .form-group.campo-xl { width: 100%; flex: 1 1 100%; }
  .form-secao-fields { flex-direction: column; align-items: stretch; }
  .form-secao-fields .span-full { flex: 1 1 100%; }
}
