/* CRM33 Comercial — base: casca vazada e o único conjunto de componentes
 * (um botão, um input, uma tabela, um selo). Áreas só acrescentam layout. */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: var(--fs-3);
  font-weight: var(--fw-1);
  line-height: 1.45;
  color: var(--ink);
  background: var(--fundo-cor) var(--fundo-img) center / cover fixed no-repeat;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--ink-soft); }
.faint { color: var(--ink-faint); }
.small { font-size: var(--fs-1); }
.strong { font-weight: var(--fw-2); }
.num { font-variant-numeric: tabular-nums; }
.row { display: flex; align-items: center; gap: 8px; }
.col { display: flex; flex-direction: column; gap: 8px; }
.grow { flex: 1 1 auto; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ico { flex: none; vertical-align: middle; }

/* ---------- Casca ---------- */
.app {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  gap: var(--gap);
  padding: var(--edge);
  height: 100dvh;
}
.rail {
  grid-row: 1 / span 2;
  background: var(--surface);
  border-radius: var(--radius);
  display: flex; flex-direction: column; align-items: center;
  padding: 10px 0; gap: 4px;
  box-shadow: var(--shadow-sm);
}
.rail-logo {
  width: 40px; height: 40px; border-radius: 10px; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-ink);
  font-weight: var(--fw-2); font-size: var(--fs-3); margin-bottom: 8px;
}
.rail-logo img { width: 100%; height: 100%; object-fit: contain; border-radius: inherit; }
.rail-item {
  width: 52px; min-height: 52px; border: 0; background: none; cursor: pointer;
  border-radius: 10px; color: var(--ink-soft);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 10.5px; position: relative; text-decoration: none;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.rail-item:hover { background: var(--surface-muted); color: var(--ink); }
.rail-item[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.rail-item .badge-count { position: absolute; top: 4px; right: 6px; }
.rail-spacer { flex: 1; }
/* Versão do sistema, logo abaixo da pessoa: pequena e discreta. No celular some: a barra de áreas
 * não tem espaço sobrando e o rótulo de cada área seria cortado. */
.rail-versao { font-size: 10px; line-height: 1.2; color: var(--ink-soft); text-align: center; max-width: 56px; padding: 2px 0 0; overflow-wrap: anywhere; user-select: none; }
@media (max-width: 767px) {
  .rail-versao { display: none; }
}

.topbar {
  background: var(--surface);
  border-radius: var(--radius);
  display: flex; align-items: center; gap: 12px; padding: 0 12px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.topbar-title { font-size: var(--fs-4); font-weight: var(--fw-2); white-space: nowrap; }
.topbar-search {
  flex: 1; max-width: 520px; height: var(--control-h);
  display: flex; align-items: center; gap: 8px; padding: 0 10px;
  border-radius: var(--radius-pill); background: var(--surface-muted); color: var(--ink-faint);
  border: 1px solid transparent; cursor: text; text-align: left;
}
.topbar-search kbd { margin-left: auto; font-size: var(--fs-1); color: var(--ink-faint); font-family: inherit; }
.health { display: flex; align-items: center; gap: 10px; }
.health-dot {
  display: inline-flex; align-items: center; gap: 6px; font-size: var(--fs-1); color: var(--ink-soft);
  padding: 4px 8px; border-radius: var(--radius-pill); background: var(--surface-soft); border: 0; cursor: default;
}
.health-dot::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.health-dot[data-state="warn"]::before { background: var(--warn); }
.health-dot[data-state="down"]::before { background: var(--danger); }
.main {
  min-width: 0; min-height: 0; overflow: hidden;
  display: flex; flex-direction: column; gap: var(--gap);
}

/* Painel: superfície branca arredondada sobre o fundo */
.panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column;
  overflow: hidden;
}
/* Cabeçalho com fundo levemente diferente do corpo: separa título e ações sem sombra */
.panel-head {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; min-height: 48px;
  background: var(--surface-soft);
  border-bottom: 1px solid var(--line);
}
.panel-title { font-size: var(--fs-4); font-weight: var(--fw-2); }
.panel-body { flex: 1; min-height: 0; overflow: auto; padding: 12px; }
.panel-foot { padding: 10px 12px; border-top: 1px solid var(--line); display: flex; gap: 8px; align-items: center; }
.page { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: var(--gap); overflow: hidden; }
.page-scroll { flex: 1; min-height: 0; overflow: auto; display: flex; flex-direction: column; gap: var(--gap); }

/* ---------- Botão ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: var(--control-h); padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  cursor: pointer; white-space: nowrap; font-weight: var(--fw-2); font-size: var(--fs-2);
  transition: background var(--dur) var(--ease), transform 60ms, opacity var(--dur);
  position: relative;
}
.btn:hover { background: var(--surface-muted); }
.btn:active { transform: scale(0.97); }
.btn:focus-visible, .input:focus-visible, .rail-item:focus-visible, .tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-muted); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn-sm { height: var(--control-h-sm); padding: 0 10px; font-size: var(--fs-1); }
.btn-icon { width: var(--control-h); padding: 0; }
.btn-sm.btn-icon { width: var(--control-h-sm); }
/* Estado de progresso dentro do próprio botão (nunca tela bloqueada) */
.btn[data-busy="1"] { pointer-events: none; }
.btn[data-busy="1"]::after {
  content: ""; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: girar 0.7s linear infinite;
}
@keyframes girar { to { transform: rotate(360deg); } }

/* ---------- Input ---------- */
.input, .select, textarea.input {
  height: var(--control-h); width: 100%;
  padding: 0 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface);
  transition: border-color var(--dur);
}
textarea.input { height: auto; min-height: 72px; padding: 8px 10px; resize: vertical; }
.input:focus, .select:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-softer); }
/* Seletores nativos no estilo do sistema (rodada 2, item 23): sem a seta do sistema operacional. */
select.select, select.input, select.hm-input {
  appearance: none; -webkit-appearance: none; padding-right: 30px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%237a6f68' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; background-size: 12px 12px;
}
:root[data-theme="dark"] select.select, :root[data-theme="dark"] select.input { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23b9aea6' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E"); }
select.select:disabled, select.input:disabled { cursor: not-allowed; opacity: .6; }
/* "X" dos campos de busca na cor da marca, no lugar do azul do navegador. */
input[type="search"]::-webkit-search-cancel-button {
  -webkit-appearance: none; appearance: none; width: 14px; height: 14px; cursor: pointer;
  background-color: var(--accent); -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M18 6 6 18M6 6l12 12'/%3E%3C/svg%3E") center / contain no-repeat; mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M18 6 6 18M6 6l12 12'/%3E%3C/svg%3E") center / contain no-repeat;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field > label, .label { font-size: var(--fs-1); color: var(--ink-soft); }
.field-hint { font-size: var(--fs-1); color: var(--ink-faint); }
.field-error { font-size: var(--fs-1); color: var(--danger); }
.search-input { position: relative; }
.search-input .ico { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--ink-faint); }
.search-input .input { padding-left: 32px; border-radius: var(--radius-pill); background: var(--surface-muted); border-color: transparent; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.switch { appearance: none; width: 34px; height: 20px; border-radius: 10px; background: var(--line-strong); position: relative; cursor: pointer; transition: background var(--dur); flex: none; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform var(--dur) var(--ease); }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(14px); }

/* ---------- Tabela ---------- */
.table-wrap { overflow: auto; min-height: 0; }
.table { width: 100%; border-collapse: collapse; font-size: var(--fs-2); }
.table th {
  position: sticky; top: 0; z-index: 1; background: var(--surface);
  text-align: left; font-weight: var(--fw-2); color: var(--ink-soft); font-size: var(--fs-1);
  height: 36px; padding: 0 10px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
.table td { height: var(--table-row-h); padding: 0 10px; border-bottom: 1px solid var(--line); }
.table tbody tr { cursor: default; transition: background var(--dur); }
.table tbody tr[data-href], .table tbody tr.clickable { cursor: pointer; }
.table tbody tr:hover { background: var(--surface-soft); }
.table tbody tr[aria-selected="true"] { background: var(--accent-soft); }
.table td.num, .table th.num { text-align: right; }

/* ---------- Selos de estado ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px;
  border-radius: var(--radius-pill); font-size: var(--fs-1); font-weight: var(--fw-2); white-space: nowrap;
  background: var(--surface-muted); color: var(--ink-soft);
}
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-draft { background: var(--draft-soft); color: var(--draft); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-accent { background: var(--accent-soft); color: var(--accent); }
.badge-count {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 11px; font-weight: var(--fw-2);
  display: inline-flex; align-items: center; justify-content: center;
}
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--ink-faint); }
.dot-ok { background: var(--ok); } .dot-warn { background: var(--warn); } .dot-danger { background: var(--danger); }
.freshness { font-size: var(--fs-1); color: var(--ink-soft); display: inline-flex; align-items: center; gap: 4px; }

/* ---------- Abas e segmentos ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); padding: 0 8px; overflow-x: auto; scrollbar-width: none; }
.tab {
  border: 0; background: none; cursor: pointer; padding: 10px 10px 9px; color: var(--ink-soft);
  border-bottom: 2px solid transparent; font-weight: var(--fw-2); font-size: var(--fs-2); white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
}
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.seg { display: inline-flex; background: var(--surface-muted); border-radius: var(--radius-pill); padding: 2px; gap: 2px; }
.seg button { border: 0; background: none; border-radius: var(--radius-pill); padding: 4px 12px; cursor: pointer; font-size: var(--fs-2); color: var(--ink-soft); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-sm); font-weight: var(--fw-2); }
.chip { display: inline-flex; align-items: center; gap: 4px; height: 26px; padding: 0 10px; border-radius: var(--radius-pill); border: 1px solid var(--line); background: var(--surface); font-size: var(--fs-1); cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* ---------- Cartões, vazios, esqueletos ---------- */
.card { border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 12px; background: var(--surface); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: var(--fs-2); }
.kv dt { color: var(--ink-soft); }
.kv dd { margin: 0; }

/* ---------- Cartão de KPI: o número manda, o rótulo sussurra ----------
 * Número em cima (maior, peso médio, algarismos tabulares); rótulo embaixo, pequeno e cinza.
 * Cor só para o que pede atenção: is-atencao (pendente) e is-alerta (atrasado/falha) trocam o
 * fundo pelo token de estado e marcam a borda com uma barra fina. O âmbar (--warn) não colore
 * texto: tem 3,6:1 sobre branco, abaixo do AA para texto normal; o alerta (--danger) passa.
 * O número encolhe com a largura do cartão (container query). */
.kpi {
  container-type: inline-size;
  display: flex; flex-direction: column; gap: 2px;
  min-width: 0; padding: 10px 12px;
  background: var(--surface-soft); border-radius: var(--radius-sm);
}
.kpi-num {
  font-size: clamp(var(--fs-4), 18cqi, calc(var(--fs-4) * 1.375)); /* 16 a 22 px conforme a largura */
  font-weight: var(--fw-2); line-height: 1.15; letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* Valor em destaque (ex.: faturamento): um degrau acima, para o olho parar nele primeiro */
.kpi-grande .kpi-num { font-size: clamp(calc(var(--fs-4) * 1.125), 13cqi, calc(var(--fs-4) * 1.75)); }
.kpi-rot { font-size: var(--fs-1); line-height: 1.3; color: var(--ink-soft); min-width: 0; }
.kpi.is-atencao { background: var(--warn-soft); box-shadow: inset 2px 0 0 var(--warn); }
.kpi.is-alerta { background: var(--danger-soft); box-shadow: inset 2px 0 0 var(--danger); }
.kpi.is-alerta .kpi-num { color: var(--danger); }
.empty { text-align: center; color: var(--ink-soft); padding: 32px 16px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.skeleton { background: linear-gradient(90deg, var(--surface-muted) 25%, var(--surface-soft) 50%, var(--surface-muted) 75%); background-size: 200% 100%; animation: brilho 1.2s infinite; border-radius: 6px; min-height: 12px; }
@keyframes brilho { to { background-position: -200% 0; } }
.divider { height: 1px; background: var(--line); margin: 8px 0; }

/* ---------- Avatar ---------- */
.avatar { width: 36px; height: 36px; border-radius: 50%; flex: none; background: var(--accent-soft); color: var(--accent); display: inline-flex; align-items: center; justify-content: center; font-weight: var(--fw-2); font-size: var(--fs-2); overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, #1a120d 40%, transparent); backdrop-filter: blur(6px);
  animation: aparecer var(--dur) var(--ease);
}
.modal { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); display: flex; flex-direction: column; overflow: hidden; }
.modal-wide { width: min(880px, calc(100vw - 32px)); }
.modal-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: var(--fs-4); font-weight: var(--fw-2); flex: 1; }
.modal-body { padding: 16px; overflow: auto; display: flex; flex-direction: column; gap: 12px; }
.modal-foot { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
@keyframes aparecer { from { opacity: 0; } }

/* ---------- Menu flutuante ---------- */
.menu { position: fixed; z-index: 60; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-sm); box-shadow: var(--shadow); padding: 4px; min-width: 180px; }
.menu button { display: flex; width: 100%; align-items: center; gap: 8px; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; text-align: left; font-size: var(--fs-2); }
.menu button:hover { background: var(--surface-muted); }

/* ---------- Avisos ---------- */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 80; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  pointer-events: auto; display: flex; align-items: center; gap: 10px;
  background: #2a211c; color: #fbf4ef; border-radius: var(--radius-sm); padding: 10px 14px;
  box-shadow: var(--shadow); font-size: var(--fs-2); max-width: min(520px, calc(100vw - 32px));
  animation: subir var(--dur) var(--ease);
}
.toast[data-variant="error"] { background: #6e2422; }
.toast[data-variant="ok"] { background: #1f4d36; }
.toast button { background: none; border: 0; color: #ffd2b9; font-weight: var(--fw-2); cursor: pointer; padding: 0; }
@keyframes subir { from { transform: translateY(8px); opacity: 0; } }
.banner { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: var(--radius-sm); font-size: var(--fs-2); }
.banner-warn { background: var(--warn-soft); color: var(--warn); }
.banner-danger { background: var(--danger-soft); color: var(--danger); }
.banner-info { background: var(--info-soft); color: var(--info); }
.net-banner { position: fixed; top: 8px; left: 50%; transform: translateX(-50%); z-index: 90; box-shadow: var(--shadow); }

/* Ponto discreto de "salvando" das ações otimistas */
.saving-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--warn); display: inline-block; animation: pulsar 1s infinite; }
.saving-dot[data-state="error"] { background: var(--danger); animation: none; }
@keyframes pulsar { 50% { opacity: 0.3; } }
.flash { animation: flash 900ms var(--ease); }
@keyframes flash { from { background: var(--accent-soft); } }
.entra { animation: entra var(--dur) var(--ease); }
@keyframes entra { from { opacity: 0; transform: translateY(-4px); } }

/* ---------- Busca global (Ctrl+K) ---------- */
.palette { width: min(640px, calc(100vw - 32px)); align-self: flex-start; margin-top: 10vh; }
.palette-list { max-height: 50vh; overflow: auto; padding: 4px; }
.palette-item { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.palette-item[aria-selected="true"], .palette-item:hover { background: var(--accent-soft); }
.palette-acoes { display: flex; gap: 6px; flex: none; }
.palette-acoes .btn[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.palette-dica { margin: 0; padding: 6px 12px 10px; }

/* ---------- Demonstração ---------- */
.demo-tag { font-size: 11px; padding: 2px 8px; border-radius: var(--radius-pill); background: var(--warn-soft); color: var(--warn); font-weight: var(--fw-2); }

/* ---------- Celular: um painel por vez + barra inferior ---------- */
@media (max-width: 767px) {
  .app { grid-template-columns: 1fr; grid-template-rows: var(--topbar-h) 1fr 60px; padding: 6px; gap: 6px; }
  .rail { grid-row: 3; flex-direction: row; justify-content: space-around; padding: 4px; overflow-x: auto; }
  .rail-logo, .rail-spacer { display: none; }
  /* Rótulo inteiro (sem "Orçame…"): o item cresce até caber; com muitos itens a barra rola de lado. */
  .rail { scroll-snap-type: x proximity; scrollbar-width: none; }
  .rail::-webkit-scrollbar { display: none; }
  .rail-item { width: auto; min-width: 48px; flex: 1 0 auto; padding: 0 4px; font-size: 10px; scroll-snap-align: start; }
  .rail-item > span:not(.badge-count) { white-space: nowrap; letter-spacing: -0.01em; }
  .topbar { gap: 6px; padding: 0 8px; overflow: hidden; }
  .topbar-title, .demo-tag, .topbar-search kbd, .topbar-search span { display: none; }
  .topbar-search { flex: 0 0 auto; width: var(--control-h); padding: 0; justify-content: center; }
  .topbar .btn-sm span.ellipsis { max-width: 110px !important; }
  .health { gap: 4px; }
  .health-dot { padding: 4px; }
  .health-dot span { display: none; }
}

/* Barras de rolagem ultrafinas e discretas (padrão CRM33) */
* { scrollbar-width: thin; scrollbar-color: color-mix(in srgb, var(--ink) 18%, transparent) transparent; }
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: color-mix(in srgb, var(--ink) 18%, transparent); border-radius: 6px; }
*::-webkit-scrollbar-thumb:hover { background: color-mix(in srgb, var(--ink) 32%, transparent); }
