/* =============================================================================
   ERP MsX — COMPONENTES
   Botão, formulário, grade, modal, toast, badge, card, abas, estado vazio.
   ============================================================================= */

/* =============================================================================
   BOTÃO
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1-5);
  height: var(--control-height-md);
  padding: 0 var(--space-3);
  font-size: var(--text-md);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  background: var(--surface-raised);
  color: var(--text-primary);
  border: 1px solid var(--border-interactive);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--duration-instant) var(--ease-out),
              border-color var(--duration-instant) var(--ease-out);
}
.btn:hover { background: var(--surface-hover); border-color: var(--border-strong); }
.btn:active { transform: translateY(0.5px); }
.btn[disabled], .btn[aria-disabled="true"] {
  opacity: 0.5; cursor: not-allowed; transform: none;
}
.btn svg { width: 14px; height: 14px; flex: none; }

.btn--primary {
  background: var(--action-primary);
  border-color: var(--action-primary);
  color: var(--text-on-accent);
}
.btn--primary:hover {
  background: var(--action-primary-hover);
  border-color: var(--action-primary-hover);
}
.btn--primary:active { background: var(--action-primary-pressed); }

.btn--ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn--ghost:hover { background: var(--surface-hover); color: var(--text-primary); border-color: transparent; }

.btn--danger { background: transparent; border-color: var(--danger-border); color: var(--danger); }
.btn--danger:hover { background: var(--danger-surface); border-color: var(--danger); }

.btn--sm { height: var(--control-height-sm); padding: 0 var(--space-2); font-size: var(--text-sm); }
.btn--lg { height: var(--control-height-lg); padding: 0 var(--space-4); }
.btn--block { width: 100%; }
.btn--icon { width: var(--control-height-md); padding: 0; }
.btn--icon.btn--sm { width: var(--control-height-sm); }

kbd {
  display: inline-grid; place-items: center;
  min-width: 18px; height: 18px;
  padding: 0 4px;
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  color: var(--text-tertiary);
  background: var(--surface-inset);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xs);
}

/* =============================================================================
   FORMULÁRIO
   ========================================================================== */

.field { display: grid; gap: var(--space-1); min-width: 0; }

.field__label {
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
}
.field__label[data-required]::after {
  content: " *";
  color: var(--danger);
}
.field__hint { font-size: var(--text-xs); color: var(--text-tertiary); }
.field__error {
  display: flex; align-items: center; gap: var(--space-1);
  font-size: var(--text-xs);
  color: var(--danger);
}

.input, .select, .textarea {
  width: 100%;
  min-width: 0;
  height: var(--control-height-md);
  padding: 0 var(--space-2);
  background: var(--surface-base);
  color: var(--text-primary);
  border: 1px solid var(--border-interactive);
  border-radius: var(--radius-sm);
  transition: border-color var(--duration-instant) var(--ease-out),
              box-shadow var(--duration-instant) var(--ease-out);
}
.textarea { height: auto; min-height: 68px; padding: var(--space-2); resize: vertical; line-height: var(--leading-snug); }
.select { padding-right: var(--space-6); appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: right 14px center, right 9px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
.input:hover, .select:hover, .textarea:hover { border-color: var(--border-strong); }
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
.input::placeholder, .textarea::placeholder { color: var(--text-disabled); }
.input[readonly], .input[disabled], .select[disabled], .textarea[disabled] {
  background: var(--surface-inset); color: var(--text-tertiary); cursor: not-allowed;
}
.input[aria-invalid="true"], .select[aria-invalid="true"], .textarea[aria-invalid="true"] {
  border-color: var(--danger);
}
.input[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 22%, transparent);
}
.input--num { text-align: right; font-variant-numeric: tabular-nums; font-feature-settings: 'tnum' 1; }

/* Grade de formulário: 12 colunas, com atalhos de span */
.form-grid {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: var(--space-3) var(--space-3);
}
.c1{grid-column:span 1}.c2{grid-column:span 2}.c3{grid-column:span 3}
.c4{grid-column:span 4}.c5{grid-column:span 5}.c6{grid-column:span 6}
.c7{grid-column:span 7}.c8{grid-column:span 8}.c9{grid-column:span 9}
.c10{grid-column:span 10}.c11{grid-column:span 11}.c12{grid-column:span 12}
@media (max-width: 720px) {
  .form-grid > [class*="c"] { grid-column: span 12; }
}

.fieldset {
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  margin: 0;
}
.fieldset > legend {
  padding: 0 var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-tertiary);
}

.switch { display: inline-flex; align-items: center; gap: var(--space-2); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch__track {
  position: relative;
  flex: none;                 /* sem isto o trilho é esmagado em container apertado */
  width: 34px; height: 18px;
  background: var(--surface-inset);
  border: 1px solid var(--border-interactive);
  border-radius: var(--radius-pill);
  transition: background var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out);
}
.switch__track::after {
  content: "";
  position: absolute; top: 2px; left: 2px;
  width: 12px; height: 12px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: transform var(--duration-fast) var(--ease-out),
              background var(--duration-fast) var(--ease-out);
}
.switch input:checked + .switch__track { background: var(--action-primary); border-color: var(--action-primary); }
.switch input:checked + .switch__track::after { transform: translateX(16px); background: #fff; }
.switch input:focus-visible + .switch__track { outline: 2px solid var(--focus-ring); outline-offset: 2px; }

/* =============================================================================
   CARD / PAINEL
   ========================================================================== */

.card {
  background: var(--surface-raised);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
}
.card__head {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
}
.card__title {
  flex: 1 1 auto;
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}
.card__body { padding: var(--space-4); }
.card__body--flush { padding: 0; }

/* =============================================================================
   GRADE DE DADOS
   ========================================================================== */

.table-wrap {
  overflow: auto;
  max-height: var(--table-max-h, none);
  border-radius: var(--radius-md);
}

.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-md);
}

.table thead th {
  position: sticky; top: 0;
  z-index: var(--z-sticky);
  height: 30px;
  padding: 0 var(--space-3);
  text-align: left;
  white-space: nowrap;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-tertiary);
  background: var(--surface-inset);
  border-bottom: 1px solid var(--border-default);
}
.table thead th[data-sortable] { cursor: pointer; user-select: none; }
.table thead th[data-sortable]:hover { color: var(--text-secondary); }
.table thead th[aria-sort] { color: var(--accent); }
.table thead th .sort-arrow { display: inline-block; margin-left: 4px; opacity: 0.8; }

.table tbody td {
  height: var(--row-cozy);
  padding: 0 var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 320px;
}
.table tbody tr:nth-child(even) { background: var(--surface-zebra); }
.table tbody tr:hover { background: var(--surface-hover); }
.table tbody tr[aria-selected="true"] { background: var(--surface-selected); }
.table tbody tr[data-clickable] { cursor: pointer; }

.table tfoot td {
  height: var(--row-cozy);
  padding: 0 var(--space-3);
  font-weight: var(--weight-semibold);
  background: var(--surface-inset);
  border-top: 1px solid var(--border-default);
  white-space: nowrap;
}

/* Coluna numérica — tabular-nums é obrigatório, senão o valor desalinha */
.table .num, .table th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum' 1, 'zero' 1;
}
.table .num--neg { color: var(--danger); }

/* Coluna de ações, fixa à direita */
.table .col-acoes {
  position: sticky; right: 0;
  width: 1%;
  text-align: right;
  background: inherit;
  box-shadow: -8px 0 8px -8px rgb(0 0 0 / 0.35);
}
.table thead .col-acoes { background: var(--surface-inset); z-index: calc(var(--z-sticky) + 1); }
.row-actions { display: inline-flex; gap: var(--space-1); }

.table--compact tbody td { height: var(--row-compact); }
.table--comfortable tbody td { height: var(--row-comfortable); }

/* Barra de ferramentas da grade */
.toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  flex-wrap: wrap;
}
.toolbar__search { position: relative; flex: 1 1 220px; max-width: 320px; }
.toolbar__search input { padding-left: 30px; }
.toolbar__search svg {
  position: absolute; left: 9px; top: 50%; transform: translateY(-50%);
  width: 14px; height: 14px; color: var(--text-tertiary); pointer-events: none;
}
.toolbar__count {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
}

/* Paginação */
.pager {
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  border-top: 1px solid var(--border-subtle);
  font-size: var(--text-sm);
  color: var(--text-tertiary);
}
.pager__spacer { flex: 1 1 auto; }
.pager strong { color: var(--text-primary); font-variant-numeric: tabular-nums; }

/* =============================================================================
   BADGE / PASTILHA DE STATUS
   ========================================================================== */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 19px;
  padding: 0 var(--space-2);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.02em;
  text-transform: uppercase;
  white-space: nowrap;
  border-radius: var(--radius-pill);
  border: 1px solid transparent;
}
.badge::before {
  content: ""; width: 5px; height: 5px; border-radius: 50%;
  background: currentColor; flex: none;
}
.badge--neutral { background: var(--surface-inset); color: var(--text-tertiary); border-color: var(--border-default); }
.badge--success { background: var(--success-surface); color: var(--success); border-color: var(--success-border); }
.badge--warning { background: var(--warning-surface); color: var(--warning); border-color: var(--warning-border); }
.badge--danger  { background: var(--danger-surface);  color: var(--danger);  border-color: var(--danger-border); }
.badge--info    { background: var(--info-surface);    color: var(--info);    border-color: var(--info-border); }
.badge--accent  { background: var(--accent-surface);  color: var(--accent);  border-color: var(--accent-border); }

/* =============================================================================
   KPI
   ========================================================================== */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}
.kpi {
  padding: var(--space-4);
  background: var(--surface-raised);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
}
.kpi__label {
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  color: var(--text-tertiary);
}
.kpi__value {
  margin-top: var(--space-2);
  font-size: 24px;
  font-weight: var(--weight-semibold);
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum' 1;
  color: var(--text-primary);
}
.kpi__delta {
  margin-top: var(--space-1-5);
  display: inline-flex; align-items: center; gap: 4px;
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}
.kpi__delta--up { color: var(--success); }
.kpi__delta--down { color: var(--danger); }
.kpi__delta--flat { color: var(--text-tertiary); }

/* =============================================================================
   GRÁFICO (SVG inline, sem biblioteca)
   ========================================================================== */

.chart { width: 100%; height: auto; overflow: visible; }
.chart__bar { fill: var(--accent); transition: fill var(--duration-instant) var(--ease-out); }
.chart__bar:hover { fill: var(--accent-hover); }
.chart__grid { stroke: var(--border-subtle); stroke-width: 1; }
.chart__axis {
  fill: var(--text-tertiary);
  font-size: 10px;
  font-family: var(--font-ui);
  font-variant-numeric: tabular-nums;
}

/* =============================================================================
   MODAL — <dialog> nativo
   ========================================================================== */

dialog.modal {
  width: min(var(--modal-w, 560px), calc(100vw - 2 * var(--space-4)));
  max-height: calc(100vh - 2 * var(--space-6));
  padding: 0;
  background: var(--surface-overlay);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-overlay);
  overflow: hidden;
}
dialog.modal::backdrop { background: rgb(0 0 0 / 0.55); backdrop-filter: blur(2px); }
dialog.modal[data-size="lg"] { --modal-w: 860px; }
dialog.modal[data-size="xl"] { --modal-w: 1120px; }

.modal__form { display: flex; flex-direction: column; max-height: inherit; min-height: 0; }
.modal__head {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  flex: none;
}
.modal__title {
  flex: 1 1 auto;
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}
.modal__body {
  padding: var(--space-4);
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}
.modal__foot {
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border-subtle);
  background: var(--surface-raised);
  flex: none;
}
.modal__foot-spacer { flex: 1 1 auto; }

/* =============================================================================
   TOAST
   ========================================================================== */

.toasts {
  position: fixed;
  right: var(--space-4); bottom: var(--space-4);
  z-index: var(--z-toast);
  display: grid;
  gap: var(--space-2);
  width: min(340px, calc(100vw - 2 * var(--space-4)));
}
.toast {
  display: flex; align-items: flex-start; gap: var(--space-2);
  padding: var(--space-3);
  background: var(--surface-overlay);
  border: 1px solid var(--border-default);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  animation: toast-in var(--duration-normal) var(--ease-out);
}
.toast--success { border-left-color: var(--success-border); }
.toast--danger  { border-left-color: var(--danger-border); }
.toast--warning { border-left-color: var(--warning-border); }
.toast__body { flex: 1 1 auto; min-width: 0; }
.toast__title { font-weight: var(--weight-semibold); }
.toast__text { color: var(--text-secondary); margin-top: 2px; }
.toast[data-leaving="true"] { animation: toast-out var(--duration-fast) var(--ease-in-out) forwards; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}
@keyframes toast-out {
  to { opacity: 0; transform: translateX(12px); }
}

/* =============================================================================
   ESTADO VAZIO / CARREGANDO
   ========================================================================== */

.empty {
  display: grid;
  place-items: center;
  gap: var(--space-3);
  padding: var(--space-12) var(--space-6);
  text-align: center;
}
.empty__mark {
  width: 48px; height: 48px;
  display: grid; place-items: center;
  border-radius: var(--radius-brand);
  background: var(--brand-gradient);
  color: #fff;
}
.empty__mark svg { width: 22px; height: 22px; }
.empty__title { font-size: var(--text-lg); font-weight: var(--weight-semibold); }
.empty__text { font-size: var(--text-sm); color: var(--text-tertiary); max-width: 46ch; }

.skeleton {
  background: linear-gradient(90deg,
    var(--surface-inset) 25%,
    var(--surface-hover) 37%,
    var(--surface-inset) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
  border-radius: var(--radius-xs);
  height: 1em;
}
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }

/* =============================================================================
   ABAS
   ========================================================================== */

.tabs {
  display: flex;
  gap: var(--space-1);
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: var(--space-4);
  overflow-x: auto;
}
.tab {
  flex: none;
  height: 32px;
  padding: 0 var(--space-3);
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  color: var(--text-tertiary);
  font-size: var(--text-md);
  font-weight: var(--weight-medium);
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--text-primary); }
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

/* =============================================================================
   PALETA DE COMANDOS (Ctrl+K)
   ========================================================================== */

dialog.palette {
  width: min(520px, calc(100vw - 2 * var(--space-4)));
  margin-top: 12vh;
  padding: 0;
  background: var(--surface-overlay);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-overlay);
  overflow: hidden;
}
dialog.palette::backdrop { background: rgb(0 0 0 / 0.5); }
.palette__input {
  width: 100%; height: 44px;
  padding: 0 var(--space-4);
  background: transparent;
  border: 0; border-bottom: 1px solid var(--border-subtle);
  font-size: var(--text-lg);
}
.palette__input:focus { outline: none; }
.palette__list { max-height: 320px; overflow-y: auto; padding: var(--space-2); }
.palette__item {
  display: flex; align-items: center; gap: var(--space-2);
  height: 34px; padding: 0 var(--space-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-md);
}
.palette__item[aria-selected="true"] { background: var(--surface-selected); color: var(--accent); }
.palette__item svg { width: 15px; height: 15px; flex: none; color: var(--text-tertiary); }
.palette__item[aria-selected="true"] svg { color: var(--accent); }
.palette__item-kind { margin-left: auto; font-size: var(--text-2xs); color: var(--text-tertiary); text-transform: uppercase; letter-spacing: var(--tracking-caps); }
.palette__empty { padding: var(--space-6); text-align: center; color: var(--text-tertiary); font-size: var(--text-sm); }

/* =============================================================================
   UTILITÁRIOS
   ========================================================================== */

.stack { display: grid; gap: var(--space-3); }
.row { display: flex; align-items: center; gap: var(--space-2); }
.row--between { justify-content: space-between; }
.row--end { justify-content: flex-end; }
.row--wrap { flex-wrap: wrap; }
.grow { flex: 1 1 auto; min-width: 0; }
.muted { color: var(--text-tertiary); }
.strong { font-weight: var(--weight-semibold); }
.tnum { font-variant-numeric: tabular-nums; font-feature-settings: 'tnum' 1; }
.mono { font-family: var(--font-mono); font-size: var(--text-sm); }
.nowrap { white-space: nowrap; }
.text-sm { font-size: var(--text-sm); }
.text-xs { font-size: var(--text-xs); }
.text-right { text-align: right; }
.mt-0 { margin-top: 0; }
.mt-2 { margin-top: var(--space-2); }
.mt-4 { margin-top: var(--space-4); }
.mt-5 { margin-top: var(--space-5); }
.hidden { display: none !important; }

.cols-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: var(--space-3); }
.cols-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: var(--space-3); }

/* Lista de definição para detalhe de registro */
.dl { display: grid; grid-template-columns: minmax(110px, auto) 1fr; gap: var(--space-2) var(--space-3); font-size: var(--text-md); }
.dl dt { color: var(--text-tertiary); font-size: var(--text-xs); align-self: center; }
.dl dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
