:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --border: #e3e8f0;
  --text: #1f2a44;
  --muted: #6b7488;
  --primary: #2b5cff;
  --primary-dark: #1e46c9;
  --green: #16a34a;
  --amber: #f59e0b;
  --orange: #ea580c;
  --red: #dc2626;
  --shadow: 0 1px 3px rgba(20, 30, 60, .08), 0 8px 24px rgba(20, 30, 60, .06);
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
.hidden { display: none !important; }

/* ---------- Login ---------- */
#login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #2b5cff, #1e46c9);
}
.login-card {
  background: var(--card);
  padding: 32px;
  border-radius: 16px;
  width: 360px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 20px; color: var(--muted); }

/* ---------- Layout ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card); padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 18px; color: var(--primary); }
.topbar-right { display: flex; align-items: center; gap: 12px; color: var(--muted); }

.tabs { display: flex; gap: 4px; padding: 12px 24px 0; }
.tab {
  padding: 10px 18px; border: none; background: transparent;
  cursor: pointer; font-size: 14px; color: var(--muted);
  border-radius: 8px 8px 0 0; font-weight: 600;
}
.tab.active { color: var(--primary); background: var(--card); box-shadow: var(--shadow); }

.container { padding: 20px 32px 60px; max-width: 1680px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }

/* ---------- Cards / grid ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-bottom: 20px; }
.cards.cards-mini { grid-template-columns: repeat(auto-fit, minmax(220px, 300px)); margin-bottom: 0; }
.lista-resumo-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
.chk-col { width: 36px; }
.tbl-atas td.chk-col, .tbl-atas th.chk-col { text-align: center; }
tr.row-selecionada td { background: #eef2ff; }
.stat {
  background: var(--card); border-radius: var(--radius); padding: 16px 18px;
  box-shadow: var(--shadow); border-left: 4px solid var(--primary);
  display: flex; align-items: center; gap: 14px;
  min-width: 0; /* permite que o item do grid encolha em telas estreitas */
}
.stat-ico { font-size: 20px; width: 44px; height: 44px; flex: 0 0 44px;
  display: grid; place-items: center; border-radius: 12px; background: #eef2ff; }
.stat-body { min-width: 0; } /* evita que texto longo (valores em R$) estoure o card */
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; white-space: nowrap; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 4px; line-height: 1.15; white-space: normal; overflow-wrap: normal; word-break: normal; }
.stat .value.valor { font-size: 17px; }
.stat.green  { border-color: var(--green); } .stat.green  .stat-ico { background: #dcfce7; }
.stat.amber  { border-color: var(--amber); } .stat.amber  .stat-ico { background: #fef3c7; }
.stat.red    { border-color: var(--red); }   .stat.red    .stat-ico { background: #fee2e2; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-2 > .card { min-width: 0; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-3 > .card { min-width: 0; }
@media (max-width: 1100px) { .grid-3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 700px) { .grid-3 { grid-template-columns: 1fr; } }

.card {
  background: var(--card); border-radius: var(--radius); padding: 18px;
  box-shadow: var(--shadow); margin-bottom: 16px;
}
.card h3 { margin: 0 0 14px; font-size: 15px; }
.chart-box { position: relative; height: 260px; }

/* ---------- Tabela ---------- */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.toolbar input, .toolbar select { min-width: 160px; }

/* Barra de filtros da lista: uma única linha, sem empilhar */
.filtros { flex-wrap: nowrap; align-items: center; }
/* anula o width:100% global e controla o tamanho pelo flex-basis */
.filtros input, .filtros select { width: auto; min-width: 0; }
.filtros .f-busca { flex: 1 1 260px; min-width: 200px; }
.filtros select { flex: 0 0 200px; }
.filtros #btn-nova { flex: 0 0 auto; margin-left: auto; }

/* Cabeçalhos ordenáveis */
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--primary); }
.sort-ind { font-size: 10px; color: var(--primary); margin-left: 2px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .03em; }
tbody tr:hover { background: #f8faff; }
td.actions { display: flex; gap: 6px; }

/* Tabela de atas: colunas de texto longo truncam com reticências */
.tbl-atas td, .tbl-atas th { vertical-align: middle; }
.cell-sub { display: block; font-size: 11px; color: var(--muted); font-weight: 400; margin-top: 2px; }
.truncate { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tbl-atas .c-obj  { max-width: 380px; }
.tbl-atas .c-forn { max-width: 280px; }
.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

.badge { padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
/* Visibilidade por perfil de acesso */
.admin-only { display: none; }
body.is-admin .admin-only { display: block; }
.editor-only { display: none !important; }
body.can-edit .editor-only { display: inline-flex !important; }

/* ---------- Configurações ---------- */
.subtabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 22px; }
.subtab { padding: 10px 18px; border: none; background: transparent; cursor: pointer;
  font: inherit; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.subtab:hover { color: var(--primary); }
.subtab.active { color: var(--primary); border-bottom-color: var(--primary); }
.subpanel { display: none; }
.subpanel.active { display: block; }

.card-sub { color: var(--muted); font-size: 13px; margin: -8px 0 18px; }
.conf-grid { display: grid; grid-template-columns: minmax(0, 420px) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 820px) { .conf-grid { grid-template-columns: 1fr; } }

.conf-ident { display: flex; gap: 28px; padding: 14px 16px; background: var(--bg);
  border-radius: 10px; margin-bottom: 18px; flex-wrap: wrap; }
.conf-lbl { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 3px; }

.perfis-legenda { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.perfis-legenda li { font-size: 13px; color: var(--text); display: flex; align-items: center; gap: 10px; }
.perfis-legenda .badge { flex: 0 0 auto; }

/* Linha de criação de usuário: campos lado a lado, alturas iguais */
.user-create { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 18px; }
.user-create input, .user-create select { width: auto; }
.user-create #nu-nome  { flex: 1 1 150px; }
.user-create #nu-email { flex: 2 1 220px; }
.user-create #nu-senha { flex: 1 1 140px; }
.user-create #nu-papel { flex: 0 0 160px; }
.user-create .btn { flex: 0 0 auto; margin-left: auto; }

#tbody-usuarios select { width: auto; padding: 6px 8px; min-width: 110px; }

.badge.admin  { background: #ede9fe; color: #5b21b6; }
.badge.gestor { background: #dbeafe; color: #1e40af; }
.badge.leitor { background: #f1f5f9; color: #475569; }
.badge.vigente   { background: #dcfce7; color: #166534; }
.badge.vencida   { background: #fee2e2; color: #991b1b; }
.badge.renovada  { background: #dbeafe; color: #1e40af; }
.badge.cancelada { background: #f1f5f9; color: #475569; }

.row-warn-30 { background: #fffbeb; }
.row-warn-15 { background: #fff7ed; }
.row-warn-7  { background: #fef2f2; }

/* ---------- Formulário ---------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 720px) { .form-grid { grid-template-columns: 1fr; } }

label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
label .req { color: var(--red); }
input, select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(43,92,255,.12); }
textarea { resize: vertical; min-height: 70px; }

.btn {
  padding: 10px 18px; border: none; border-radius: 8px; cursor: pointer;
  font: inherit; font-weight: 600; background: var(--primary); color: #fff;
}
.btn:hover { background: var(--primary-dark); }
.btn.ghost { background: transparent; color: var(--primary); border: 1px solid var(--border); }
.btn.ghost:hover { background: #f0f4ff; }
.btn.danger { background: var(--red); }
.btn.sm { padding: 6px 10px; font-size: 12px; }
.btn:disabled { opacity: .6; cursor: not-allowed; }

.form-actions { display: flex; gap: 10px; margin-top: 18px; }
.error { color: var(--red); font-size: 13px; min-height: 18px; }
.hint { color: var(--muted); font-size: 12px; }

/* ---------- Upload ---------- */
.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 26px; text-align: center; color: var(--muted); cursor: pointer;
  transition: .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--primary); background: #f0f4ff; color: var(--primary); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 24px; right: 24px; z-index: 50;
  background: #1f2a44; color: #fff; padding: 12px 18px; border-radius: 10px;
  box-shadow: var(--shadow); opacity: 0; transform: translateY(10px); transition: .2s;
}
#toast.show { opacity: 1; transform: translateY(0); }
#toast.ok  { background: #16653d; }
#toast.err { background: #991b1b; }

.spinner {
  display: inline-block; width: 16px; height: 16px; border: 2px solid #fff;
  border-top-color: transparent; border-radius: 50%; animation: spin .7s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }
