/* =========================================================================
   Sentinela Tributário — Design System (CONTEXTO §3)
   Paleta azul-escura institucional. Tokens em :root.
   ========================================================================= */

:root {
  --primary:        #1B3A6B;
  --primary-mid:    #2E5BA8;
  --primary-light:  #E8EEF7;
  --danger:         #C0392B;
  --danger-light:   #FCEBEB;
  --warning:        #E67E22;
  --warning-light:  #FFF3CD;
  --success:        #0F6E56;
  --success-light:  #E1F5EE;
  --text-primary:   #111;
  --text-secondary: #555;
  --text-muted:     #888;
  --border:         #e5e5e5;
  --border-light:   #f0f0f0;
  --bg-page:        #f0f4f8;
  --bg-card:        #fff;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--bg-page);
}

.hidden { display: none !important; }

/* ───────────────────────── Login ───────────────────────── */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #eaf0f8 0%, #f0f4f8 60%, #e7edf6 100%);
  padding: 24px;
}
.login-card {
  width: 340px;
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px;
  box-shadow: 0 12px 40px rgba(27, 58, 107, 0.10);
  text-align: center;
}
.login-logo {
  width: 52px; height: 52px;
  margin: 0 auto 14px;
  border-radius: 14px;
  background: var(--primary);
  color: #fff;
  font-weight: 700; font-size: 19px;
  display: flex; align-items: center; justify-content: center;
}
.login-title { font-size: 20px; font-weight: 700; color: var(--primary); }
.login-sub   { font-size: 12px; color: var(--text-muted); margin-top: 2px; margin-bottom: 18px; }
.login-error { color: var(--danger); font-size: 12px; margin-top: 10px; min-height: 16px; }

.field { display: block; text-align: left; margin-bottom: 12px; }
.field > span {
  display: block;
  font-size: 11px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.03em;
  margin-bottom: 4px;
}

/* ───────────────────────── Inputs ───────────────────────── */
input[type="text"], input[type="password"], input[type="number"],
input[type="date"], select, textarea {
  width: 100%;
  background: var(--bg-card);
  border: 0.5px solid #ccc;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text-primary);
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(27, 58, 107, 0.12);
}
label.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-secondary); }
label.check input { width: auto; }

/* ───────────────────────── Botões ───────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--bg-card);
  color: var(--text-secondary);
  border: 0.5px solid #ccc;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px; font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: background .12s, color .12s, border-color .12s;
}
.btn:hover { background: #f6f8fb; }
a.btn { text-decoration: none; }
.acoes { display: flex; gap: 6px; justify-content: flex-end; white-space: nowrap; }
table.diff { border-collapse: collapse; font-size: 12px; }
table.diff td { padding: 1px 8px 1px 0; vertical-align: top; }
.btn-primary { background: var(--primary); color: #fff; border: none; }
.btn-primary:hover { background: #142d54; }
.btn-ghost { background: transparent; border: 0.5px solid var(--border); color: var(--text-secondary); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ───────────────────────── Layout do app ───────────────────────── */
.app-view { display: flex; min-height: 100vh; }

.sidebar {
  width: 210px; min-width: 210px;
  background: var(--bg-card);
  border-right: 0.5px solid var(--border);
  display: flex; flex-direction: column;
  padding: 16px 12px;
  position: sticky; top: 0; height: 100vh; overflow: hidden;
}
.sb-logo-wrap { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.sb-logo {
  width: 32px; height: 32px; flex: 0 0 32px;
  border-radius: 8px; background: var(--primary);
  color: #fff; font-weight: 700; font-size: 13px;
  display: flex; align-items: center; justify-content: center;
}
.sb-brand { font-size: 13px; font-weight: 700; color: var(--primary); line-height: 1.1; }
.sb-brand small { display: block; font-size: 9px; color: #aaa; font-weight: 600; margin-top: 2px; }

.sb-nav { flex: 1 1 auto; min-height: 0; overflow-y: auto; margin-top: 12px; }
.sb-nav::-webkit-scrollbar { width: 6px; }
.sb-nav::-webkit-scrollbar-thumb { background: #d4d9e2; border-radius: 3px; }
.sb-grupo { margin-bottom: 2px; }
.sb-secao {
  display: flex; align-items: center; gap: 8px;
  width: 100%;
  background: transparent; border: none;
  text-align: left; cursor: pointer; font-family: inherit;
  padding: 7px 8px; border-radius: 8px; margin-top: 8px;
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.07em;
  color: #9aa3b2; font-weight: 700;
}
.sb-secao:hover { background: #f4f6fa; color: var(--primary); }
.sb-secao.tem-ativa { color: var(--primary); }
.sb-secao .ico { font-size: 13px; width: 16px; text-align: center; }
.sb-secao-nome { flex: 1 1 auto; }
.sb-seta {
  font-size: 10px; color: #b8bfca;
  transition: transform 0.15s ease;
}
.sb-grupo:not(.recolhida) .sb-seta { transform: rotate(90deg); }

/* Itens da seção: animam altura ao recolher/expandir. */
.sb-itens {
  display: flex; flex-direction: column; gap: 1px;
  overflow: hidden; max-height: 800px;
  transition: max-height 0.2s ease, opacity 0.15s ease;
}
.sb-grupo.recolhida .sb-itens { max-height: 0; opacity: 0; }

.sb-item {
  display: flex; align-items: center; gap: 9px;
  width: 100%;
  background: transparent; border: none;
  text-align: left;
  padding: 8px 12px 8px 18px;
  border-radius: 8px;
  font-size: 13px; font-weight: 500; color: var(--text-secondary);
  cursor: pointer;
  font-family: inherit;
}
.sb-item:hover { background: #f4f6fa; color: var(--primary); }
.sb-item.active {
  background: var(--primary-light); color: var(--primary); font-weight: 600;
  border-right: 2px solid var(--primary);
}
.sb-item .ico { font-size: 14px; width: 18px; text-align: center; }

.sb-rodape {
  display: flex; align-items: center; gap: 8px;
  padding-top: 10px; margin-top: 8px;
  border-top: 0.5px solid var(--border-light);
}
.sb-avatar {
  width: 26px; height: 26px; flex: 0 0 26px;
  border-radius: 50%; background: var(--primary);
  color: #fff; font-size: 10px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.sb-user { font-size: 11px; color: var(--text-secondary); line-height: 1.15; }
.sb-user small { display: block; color: #aaa; font-size: 9px; text-transform: capitalize; }
.sb-rodape + .btn { margin-top: 8px; }

.content { flex: 1; min-width: 0; }
.page { max-width: 1400px; margin: 0 auto; padding: 24px 24px 32px; }

/* Barra superior + overlay do menu-gaveta: ocultos no desktop, ativados no @media abaixo. */
.topbar { display: none; }
.sb-overlay { display: none; }

/* ───────────────────────── Cabeçalho de página ───────────────────────── */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.page-title { font-size: 18px; font-weight: 700; color: var(--text-primary); }
.page-title .emoji { margin-right: 6px; }

/* ───────────────────────── Seções ───────────────────────── */
.secao-sep {
  background: #f8f8f8;
  border: 0.5px solid var(--border);
  border-left: 3px solid var(--primary);
  border-radius: 8px;
  padding: 9px 14px;
  margin: 16px 0 10px;
  font-size: 12px; font-weight: 600; color: var(--text-secondary);
}
.secao-sep.red    { border-left-color: var(--danger); }
.secao-sep.orange { border-left-color: var(--warning); }
.secao-sep.green  { border-left-color: var(--success); }

/* ───────────────────────── KPI cards ───────────────────────── */
.kpi-grid { display: grid; gap: 12px; margin: 4px 0 8px; }
.kpi-grid.c2 { grid-template-columns: repeat(2, 1fr); }
.kpi-grid.c3 { grid-template-columns: repeat(3, 1fr); }
.kpi-grid.c4 { grid-template-columns: repeat(4, 1fr); }

.kpi-card {
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  border-left: 3px solid var(--primary);
}
.kpi-card.blue   { border-left-color: var(--primary); }
.kpi-card.mid    { border-left-color: var(--primary-mid); }
.kpi-card.red    { border-left-color: var(--danger); }
.kpi-card.orange { border-left-color: var(--warning); }
.kpi-card.green  { border-left-color: var(--success); }
.kpi-label { font-size: 11px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.kpi-value { font-size: 22px; font-weight: 700; color: var(--text-primary); margin-top: 4px; }
.kpi-sub   { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

/* ───────────────────────── Card genérico ───────────────────────── */
.card {
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 12px;
}
.card.tight { padding: 14px 16px; }

/* ───────────────────────── Badges ───────────────────────── */
.badge {
  display: inline-block;
  border-radius: 20px;
  padding: 3px 10px;
  font-size: 11px; font-weight: 600; line-height: 1.4;
}
.badge.urgente      { background: var(--danger-light);  color: #A32D2D; }
.badge.atencao      { background: var(--warning-light); color: #856404; }
.badge.oportunidade { background: var(--success-light); color: #0a5741; }
.badge.info         { background: var(--primary-light); color: var(--primary); }
.badge.doc-recebida { background: var(--primary-light); color: var(--primary); font-weight: 500; }
.badge.doc-emitida  { background: var(--success-light); color: #0a5741; font-weight: 500; }
.badge.doc-cte      { background: var(--warning-light); color: #856404; font-weight: 500; }
.badge.doc-nfse     { background: #F3E8FF; color: #6b21a8; font-weight: 500; }

/* ───────────────────────── Cards de alerta (radar) ───────────────────────── */
.alerta-card {
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-left: 3px solid var(--text-muted);
  border-radius: 12px;
  padding: 14px 16px;
  margin-bottom: 10px;
}
.alerta-card.urgente      { border-left-color: var(--danger); }
.alerta-card.atencao      { border-left-color: var(--warning); }
.alerta-card.oportunidade { border-left-color: var(--success); }
.alerta-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 6px; }
.alerta-titulo { font-size: 14px; font-weight: 700; color: var(--text-primary); }
.alerta-meta { font-size: 11px; color: var(--text-muted); white-space: nowrap; }
.alerta-desc { font-size: 13px; color: var(--text-secondary); line-height: 1.5; }
.alerta-acao { font-size: 13px; color: var(--primary); margin-top: 6px; font-weight: 500; }

/* ───────────────────────── Tabela ───────────────────────── */
.table-wrap {
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: 12px;
  overflow-x: auto;            /* tabelas largas rolam na horizontal (essencial no celular) */
  -webkit-overflow-scrolling: touch;  /* rolagem com inércia no Safari iOS */
}
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
thead th {
  text-align: left;
  font-size: 11px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.03em;
  padding: 10px 14px;
  background: #fafbfc;
  border-bottom: 0.5px solid var(--border);
}
tbody td { padding: 10px 14px; border-bottom: 0.5px solid var(--border-light); color: var(--text-secondary); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafbfd; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ───────────────────────── Filtros ───────────────────────── */
.filtros { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.filtros .field { margin-bottom: 0; }

/* ───────────────────────── Radio pills ───────────────────────── */
.pills { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 12px; }
.pill {
  border: 0.5px solid var(--border);
  background: var(--bg-card);
  border-radius: 20px;
  padding: 5px 14px;
  font-size: 12px; color: var(--text-secondary);
  cursor: pointer;
}
.pill.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ───────────────────────── Formulários em grade ───────────────────────── */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; margin-top: 14px; }

/* ───────────────────────── Estados ───────────────────────── */
.muted { color: var(--text-muted); }
.empty {
  text-align: center; color: var(--text-muted);
  padding: 28px; background: var(--bg-card);
  border: 0.5px dashed var(--border); border-radius: 12px; font-size: 13px;
}
.toast {
  position: fixed; bottom: 20px; right: 20px;
  background: var(--primary); color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.18);
  opacity: 0; transform: translateY(8px);
  transition: opacity .18s, transform .18s; z-index: 1000;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.err { background: var(--danger); }

/* ───────────────────────── XML / code ───────────────────────── */
pre.xml {
  background: #0f1b2d; color: #cfe0f5;
  border-radius: 10px; padding: 14px;
  font-size: 12px; line-height: 1.5;
  overflow: auto; max-height: 360px;
  font-family: 'Cascadia Code', Consolas, monospace;
  white-space: pre-wrap; word-break: break-word;
}

/* ───────────────────────── Modal ───────────────────────── */
.modal-bg {
  position: fixed; inset: 0; background: rgba(17,29,45,.45);
  display: flex; align-items: center; justify-content: center; z-index: 900; padding: 24px;
}
.modal {
  background: var(--bg-card); border-radius: 16px;
  width: 100%; max-width: 640px; max-height: 86vh; overflow: auto;
  padding: 22px 24px; box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-title { font-size: 15px; font-weight: 700; color: var(--primary); }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text-muted); }

/* ───────────────────────── Responsivo ───────────────────────── */
@media (max-width: 860px) {
  .kpi-grid.c4 { grid-template-columns: repeat(2, 1fr); }
  .kpi-grid.c3 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ===== Celular / tablet estreito: menu vira gaveta lateral ===== */
@media (max-width: 768px) {
  /* Barra superior fixa com botão de menu e título da tela. */
  .topbar {
    display: flex; align-items: center; gap: 12px;
    position: sticky; top: 0; z-index: 150;
    background: var(--bg-card);
    border-bottom: 0.5px solid var(--border);
    padding: 10px 16px;
    padding-top: calc(10px + env(safe-area-inset-top));  /* respeita o notch do iPhone */
  }
  .topbar-btn {
    background: none; border: none; cursor: pointer;
    font-size: 24px; line-height: 1; color: var(--primary);
    padding: 2px 6px; margin: -2px 0;
  }
  .topbar-title { font-size: 15px; font-weight: 700; color: var(--primary); }

  /* Sidebar sai do fluxo e desliza a partir da esquerda. */
  .sidebar {
    position: fixed; top: 0; left: 0; z-index: 200;
    width: 250px; min-width: 250px; height: 100vh;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 0 0 40px rgba(17, 29, 45, 0.18);
    padding-top: calc(16px + env(safe-area-inset-top));
  }
  .sidebar.open { transform: translateX(0); }

  /* Fundo escurecido quando o menu está aberto. */
  .sb-overlay {
    position: fixed; inset: 0; z-index: 180;
    background: rgba(17, 29, 45, 0.45);
  }
  .sb-overlay.show { display: block; }

  /* Mais respiro de conteúdo e alvos de toque maiores. */
  .page { padding: 16px 14px calc(28px + env(safe-area-inset-bottom)); }
  .page-head { flex-wrap: wrap; gap: 8px; }
  .sb-item { padding-top: 11px; padding-bottom: 11px; }
  .modal-bg { padding: 12px; }
  .acoes { flex-wrap: wrap; }

  /* Cards com menos respiro lateral no celular (Receita D do guia). */
  .card { padding: 16px; overflow-x: auto; }   /* overflow-x: rede de segurança p/ tabela larga sem wrap */
  .card.tight { padding: 12px 14px; }

  /* Botões de ação do cabeçalho ocupam a largura quando quebram a linha. */
  .page-head > div { display: flex; flex-wrap: wrap; gap: 8px; }

  /* Filtros empilham com toque confortável. */
  .filtros { grid-template-columns: 1fr; }

  /* iOS Safari dá zoom automático em campos com fonte < 16px ao focar.
     Forçar 16px evita o salto de zoom indesejado no formulário. */
  input[type="text"], input[type="password"], input[type="number"],
  input[type="date"], select, textarea {
    font-size: 16px;
  }
}

/* ===== Telas bem estreitas (≈ celular em pé): KPIs em coluna única ===== */
@media (max-width: 480px) {
  .kpi-grid.c2, .kpi-grid.c4 { grid-template-columns: 1fr; }
}

/* iOS Safari: altura dinâmica da viewport (100dvh) evita corte com a barra de
   endereço que sobe/desce. Mantém 100vh como fallback nos navegadores sem suporte. */
@supports (height: 100dvh) {
  .app-view { min-height: 100dvh; }
  @media (max-width: 768px) {
    .sidebar { height: 100dvh; }
  }
}
