/* ============================================================================
   base.css — reset, tipografia, shell (SideNav + Topbar + Page header)
   Composição: docs/wireframe-composicao-paginas.md Seção 2
   Valores: docs/design-system.md (nenhum valor literal fora de tokens.css)
   ========================================================================== */

*, *::before, *::after { box-sizing: border-box; }

html { font-size: 100%; -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--fs-body);
  line-height: var(--lh-body);
  font-weight: var(--fw-regular);
  color: var(--ink-900);
  background: var(--surface-page);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4, p, figure, ul, ol, dl { margin: 0; }
ul, ol { padding: 0; list-style: none; }
img, svg { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }

/* Foco visível SEMPRE — design-system.md 4.4. Proibido outline:none sem
   substituto equivalente. */
:focus-visible {
  outline: 2px solid var(--brand-600);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* Algarismo tabular: aplicado por classe, nunca por herança global (2.3) */
.num { font-variant-numeric: tabular-nums; }
.mono { font-family: var(--font-mono); }

/* Escala tipográfica — classes semânticas, sem framework CSS */
.t-kpi        { font-size: var(--fs-kpi); line-height: var(--lh-kpi); font-weight: var(--fw-semibold); letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.t-page-title { font-size: var(--fs-page-title); line-height: var(--lh-page-title); font-weight: var(--fw-semibold); letter-spacing: -.01em; }
.t-section    { font-size: var(--fs-section); line-height: var(--lh-section); font-weight: var(--fw-semibold); }
.t-card-title { font-size: var(--fs-card-title); line-height: var(--lh-card-title); font-weight: var(--fw-semibold); }
.t-body       { font-size: var(--fs-body); line-height: var(--lh-body); }
.t-table      { font-size: var(--fs-table); line-height: var(--lh-table); }
.t-table-emph { font-size: var(--fs-table); line-height: var(--lh-table); font-weight: var(--fw-medium); font-variant-numeric: tabular-nums; }
.t-label      { font-size: var(--fs-label); line-height: var(--lh-label); font-weight: var(--fw-medium); }
.t-caption    { font-size: var(--fs-caption); line-height: var(--lh-caption); }
.t-badge      { font-size: var(--fs-badge); line-height: var(--lh-badge); font-weight: var(--fw-medium); }

.ink-600 { color: var(--ink-600); }
.ink-400 { color: var(--ink-400); }
.pos { color: var(--pos-700); }
.neg { color: var(--neg-700); }
.warn { color: var(--warn-700); }
.measure { max-width: 68ch; }   /* texto corrido nunca passa de 68ch (2.3) */

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.icon { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none;
        stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.icon-sm { width: 16px; height: 16px; }
.icon-xs { width: 12px; height: 12px; }
.icon-lg { width: 32px; height: 32px; }

/* ============================ SHELL ====================================== */

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

/* ---- SideNav (design-system.md 6.1) ------------------------------------- */
.sidenav {
  width: var(--nav-width);
  flex: none;
  background: var(--nav-bg);
  color: var(--nav-text-idle);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidenav__brand { padding: var(--space-4) var(--space-3) var(--space-2); }
/* Wordmark é TEXTO PURO — nenhum logo/símbolo inventado (Constitution Art. IV,
   design-system.md 6.1 e 11.2.1). "Prumo" é nome provisório. */
.wordmark {
  color: var(--nav-text-active);
  font-size: var(--fs-card-title);
  line-height: var(--lh-card-title);
  font-weight: var(--fw-semibold);
  letter-spacing: -.01em;
  padding: 0 var(--space-2);
}

.sidenav__divider { height: 1px; background: var(--nav-bg-hover); margin: var(--space-3) var(--space-3) var(--space-2); }

.nav-groups { padding: 0 var(--space-3) var(--space-4); flex: 1; }
.nav-group + .nav-group { margin-top: var(--space-4); }
.nav-group__label {
  color: var(--nav-text-idle);
  font-size: var(--fs-label); line-height: var(--lh-label); font-weight: var(--fw-medium);
  padding: 0 var(--space-3) var(--space-2);
}

.nav-item {
  display: flex; align-items: center; gap: var(--space-2);
  height: var(--control-height); padding: 0 var(--space-3);
  border-radius: var(--radius-md);
  color: var(--nav-text-idle);
  font-size: var(--fs-table); line-height: var(--lh-table); font-weight: var(--fw-medium);
  text-decoration: none;
  position: relative;
  transition: background var(--motion-conferencia) var(--ease-out),
              color var(--motion-conferencia) var(--ease-out);
}
.nav-item + .nav-item { margin-top: 2px; }
.nav-item:hover { background: var(--nav-bg-hover); color: var(--nav-text-active); }
.nav-item[aria-current="page"] { background: var(--brand-700); color: var(--nav-text-active); }
/* Barra de 2px = indicação de POSIÇÃO em lista vertical (uso funcional,
   distinto da faixa decorativa proibida em design-system.md 5.3). */
.nav-item[aria-current="page"]::before {
  content: ""; position: absolute; left: 0; top: 8px; bottom: 8px;
  width: 2px; background: var(--nav-text-active); border-radius: var(--radius-full);
}
.nav-item__label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sidenav__footer { border-top: 1px solid var(--nav-bg-hover); padding: var(--space-3); }
.user-menu {
  display: flex; align-items: center; gap: var(--space-2); width: 100%;
  min-height: var(--touch-target); padding: var(--space-2);
  background: transparent; border: 0; border-radius: var(--radius-md);
  color: var(--nav-text-active); cursor: pointer; text-align: left;
  transition: background var(--motion-conferencia) var(--ease-out);
}
.user-menu:hover { background: var(--nav-bg-hover); }
.avatar {
  width: 28px; height: 28px; flex: none; border-radius: var(--radius-full);
  background: var(--brand-700); color: #FFF;
  display: grid; place-items: center;
  font-size: var(--fs-badge); font-weight: var(--fw-medium);
}

/* ---- CompanySwitcher (design-system.md 6.1.1) --------------------------- */
.company-switcher { position: relative; padding: 0 var(--space-3); }
.company-switcher__button {
  display: flex; align-items: center; gap: var(--space-2);
  width: 100%; min-height: var(--touch-target);
  padding: var(--space-2) var(--space-3);
  background: transparent; border: 0; border-radius: var(--radius-md);
  color: var(--nav-text-active); cursor: pointer; text-align: left;
  transition: background var(--motion-conferencia) var(--ease-out);
}
.company-switcher__button:hover { background: var(--nav-bg-hover); }
.company-avatar {
  width: 28px; height: 28px; flex: none; border-radius: var(--radius-sm);
  background: var(--brand-700); color: #FFF;
  display: grid; place-items: center;
  font-size: var(--fs-badge); line-height: var(--lh-badge); font-weight: var(--fw-medium);
}
.company-switcher__text { min-width: 0; flex: 1; overflow: hidden; }
.company-switcher__name {
  font-size: var(--fs-table); line-height: var(--lh-table); font-weight: var(--fw-medium);
  color: var(--nav-text-active);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* CNPJ é o desambiguador real entre empresas de nome parecido (6.1.1) */
.company-switcher__cnpj {
  font-size: var(--fs-caption); line-height: var(--lh-caption);
  color: var(--nav-text-idle); font-variant-numeric: tabular-nums;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.company-switcher__chevron {
  transition: transform var(--motion-conferencia) var(--ease-out);  /* Conferência */
}
.company-switcher__button[aria-expanded="true"] .company-switcher__chevron { transform: rotate(180deg); }

.company-list {
  position: absolute; z-index: 40; top: calc(100% + var(--space-1));
  left: var(--space-3); width: calc(var(--nav-width) - var(--space-6) + 40px);
  background: var(--surface-card); color: var(--ink-900);
  border-radius: var(--radius-lg); box-shadow: var(--elev-1);
  padding: var(--space-2); display: none;
}
.company-list[data-open="true"] { display: block; }
.company-list__item {
  display: flex; align-items: center; gap: var(--space-2);
  width: 100%; min-height: var(--touch-target);
  padding: var(--space-2); border: 0; background: transparent;
  border-radius: var(--radius-md); cursor: pointer; text-align: left;
}
.company-list__item:hover { background: var(--surface-page); }
.company-list__item[aria-selected="true"] { background: var(--brand-subtle); color: var(--brand-700); }
.company-list__name { font-size: var(--fs-table); font-weight: var(--fw-medium); }
.company-list__cnpj { font-size: var(--fs-caption); color: var(--ink-400); font-variant-numeric: tabular-nums; }

/* ---- Área principal ----------------------------------------------------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* Topbar 56px sticky (design-system.md 5.4 / wireframe 2.2) */
.topbar {
  height: var(--topbar-height); flex: none;
  position: sticky; top: 0; z-index: 30;
  background: var(--surface-card);
  border-bottom: 1px solid var(--border-subtle);
  display: flex; align-items: center; gap: var(--space-3);
  padding: 0 var(--space-8);
}
.topbar__spacer { flex: 1; }
.icon-button {
  /* inline-grid, não grid: `display: grid` gera caixa de nível de bloco e
     quebrava a coluna "Ações" da tabela em 3 linhas (medido em Chrome),
     estourando a altura de linha de 48px do design system 6.3. */
  display: inline-grid; place-items: center; vertical-align: middle;
  width: var(--touch-target); height: var(--touch-target);
  background: transparent; border: 0; border-radius: var(--radius-md);
  color: var(--ink-600); cursor: pointer; position: relative;
  transition: background var(--motion-conferencia) var(--ease-out);
}
.icon-button:hover { background: var(--surface-page); }
.badge-count {
  position: absolute; top: 6px; right: 6px;
  min-width: 16px; height: 16px; padding: 0 4px;
  background: var(--neg-700); color: #FFF;
  border-radius: var(--radius-full);
  font-size: 10px; line-height: 16px; font-weight: var(--fw-medium); text-align: center;
}
.menu-toggle { display: none; }

/* Conteúdo */
.content {
  flex: 1; width: 100%; max-width: var(--content-max);
  margin: 0 auto; padding: var(--space-8);
}

/* Page header (wireframe 2.3) — sem badge/pill decorativo acima do título */
.page-header {
  display: flex; align-items: flex-start; gap: var(--space-4);
  margin-bottom: var(--space-6); flex-wrap: wrap;
}
.page-header__text { flex: 1; min-width: 260px; }
.page-header__actions { display: flex; gap: var(--space-2); align-items: center; }
.page-header p { margin-top: var(--space-1); }

/* Blocos de seção */
.section { margin-top: var(--space-10); }
.section > .t-section { margin-bottom: var(--space-4); }
.stack-4 > * + * { margin-top: var(--space-4); }
.stack-6 > * + * { margin-top: var(--space-6); }

/* Grid de 12 colunas, gap space-6 (design-system.md 5.4) */
.grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: var(--space-6); }
.col-3 { grid-column: span 3; }
.col-4 { grid-column: span 4; }
.col-6 { grid-column: span 6; }
.col-8 { grid-column: span 8; }
.col-12 { grid-column: span 12; }

/* Faixa de KPI: 4 / 2 / 1 colunas (design-system.md 5.4) */
.kpi-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--space-4); }
.kpi-row--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.card-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-4); }
.card-grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

/* ============================ RESPONSIVO ================================= */

/* md: 768–1279px — nav colapsada 64px, KPIs em 2 colunas */
@media (max-width: 1279px) {
  .sidenav { width: var(--nav-width-collapsed); }
  .sidenav .wordmark,
  .nav-group__label,
  .nav-item__label,
  .company-switcher__text,
  .company-switcher__chevron,
  .user-menu__text { display: none; }
  .nav-item, .company-switcher__button, .user-menu { justify-content: center; padding-left: 0; padding-right: 0; }
  .company-list { left: var(--space-2); width: 280px; }
  .kpi-row, .kpi-row--3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .card-grid, .card-grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .col-8, .col-4, .col-6, .col-3 { grid-column: span 12; }
  .content { padding: var(--space-6); }
  .topbar { padding: 0 var(--space-6); }
}

/* sm: < 768px — nav vira drawer sobre overlay, KPIs 1 coluna */
@media (max-width: 767px) {
  .sidenav {
    position: fixed; inset: 0 auto 0 0; z-index: 60;
    width: var(--nav-width);
    transform: translateX(-100%);
    transition: transform var(--motion-lancamento) var(--ease-out);
  }
  .sidenav[data-open="true"] { transform: translateX(0); }
  .sidenav .wordmark,
  .nav-group__label,
  .nav-item__label,
  .company-switcher__text,
  .company-switcher__chevron,
  .user-menu__text { display: block; }
  .nav-item, .company-switcher__button, .user-menu { justify-content: flex-start; padding-left: var(--space-3); padding-right: var(--space-3); }
  .nav-overlay {
    position: fixed; inset: 0; z-index: 50;
    background: rgba(26, 29, 35, .45);
    display: none;
  }
  .nav-overlay[data-open="true"] { display: block; }
  .menu-toggle { display: inline-grid; }
  .kpi-row, .kpi-row--3, .card-grid, .card-grid--2 { grid-template-columns: minmax(0, 1fr); }
  .content { padding: var(--space-4); }
  .topbar { padding: 0 var(--space-4); }
  .page-header { flex-direction: column; }
}
