/* Componentes base. Depende de tokens.css e base.css. */

/* ---------- Botões ---------- */
.btn {
  --btn-bg: var(--surface); --btn-fg: var(--ink); --btn-line: var(--line-strong);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  min-height: var(--control-h); padding: 0 var(--s-4);
  border: 1px solid var(--btn-line); border-radius: var(--r-sm);
  background: var(--btn-bg); color: var(--btn-fg);
  font-weight: 550; font-size: var(--text-sm); line-height: 1; text-decoration: none; white-space: nowrap;
  transition: transform var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:active { transform: scale(.97); }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn--primary { --btn-bg: var(--primary); --btn-fg: var(--on-primary); --btn-line: var(--primary); }
.btn--primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn--primary:active { background: var(--primary-press); }
.btn--ghost { --btn-bg: transparent; --btn-line: transparent; }
.btn--ghost:hover { background: var(--surface-sunk); }
.btn--danger { --btn-fg: var(--neg-ink); --btn-line: var(--line-strong); }
.btn--danger:hover { background: var(--neg-bg); }
.btn--sm { min-height: var(--control-h-sm); padding: 0 var(--s-3); }
.btn--lg { min-height: 48px; padding: 0 var(--s-6); font-size: var(--text-base); }
.btn--block { width: 100%; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; transform: none; }
.btn[aria-busy="true"] { color: transparent; position: relative; pointer-events: none; }
.btn[aria-busy="true"]::after { content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--btn-fg); border-right-color: transparent; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(1turn); } }

/* Botão só com ícone: alvo de 40px, rótulo obrigatório via aria-label */
.icon-btn {
  display: inline-grid; place-items: center; width: var(--control-h); height: var(--control-h);
  border: 0; border-radius: var(--r-sm); background: transparent; color: var(--ink-2);
  transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.icon-btn:hover { background: var(--surface-sunk); color: var(--ink); }
.icon-btn:active { transform: scale(.94); }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn--sm { width: var(--control-h-sm); height: var(--control-h-sm); }

/* ---------- Campos ---------- */
.field { display: grid; gap: var(--s-1); }
.field__label { font-size: var(--text-sm); font-weight: 550; color: var(--ink); }
.field__hint { font-size: var(--text-xs); color: var(--ink-2); }
.field__error { font-size: var(--text-xs); color: var(--neg-ink); display: flex; gap: var(--s-1); align-items: flex-start; }
.field__error svg { width: 14px; height: 14px; flex: none; margin-top: 2px; }
.input, .select, .textarea {
  width: 100%; min-height: var(--control-h); padding: 0 var(--s-3);
  border: 1px solid var(--line-strong); border-radius: var(--r-sm);
  background: var(--surface); color: var(--ink);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.textarea { padding: var(--s-2) var(--s-3); min-height: 88px; resize: vertical; line-height: var(--leading); }
.input::placeholder, .textarea::placeholder { color: var(--ink-3); }
.input:hover, .select:hover, .textarea:hover { border-color: var(--ink-2); }
.input:focus-visible, .select:focus-visible, .textarea:focus-visible { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px var(--primary-bg); }
.input[aria-invalid="true"], .select[aria-invalid="true"], .textarea[aria-invalid="true"] { border-color: var(--neg); }
.input[aria-invalid="true"]:focus-visible { box-shadow: 0 0 0 3px var(--neg-bg); }
.input:disabled, .select:disabled { background: var(--surface-sunk); color: var(--ink-3); cursor: not-allowed; }
.select { appearance: none; padding-right: var(--s-8); background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='%235C6066' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m3 4.5 3 3 3-3'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; }

/* Campo de dinheiro: prefixo R$ como texto, não ícone decorativo */
.money-input { position: relative; }
.money-input__prefix { position: absolute; left: var(--s-3); top: 50%; transform: translateY(-50%); color: var(--ink-2); font-size: var(--text-sm); pointer-events: none; }
.money-input .input { padding-left: 38px; text-align: right; font-variant-numeric: tabular-nums; }

/* Busca: único campo com ícone interno */
.search { position: relative; }
.search svg { position: absolute; left: var(--s-3); top: 50%; width: 16px; height: 16px; transform: translateY(-50%); color: var(--ink-3); pointer-events: none; }
.search .input { padding-left: 36px; }

/* ---------- Checkbox "pago" com traço animado ---------- */
.check { display: inline-flex; align-items: center; gap: var(--s-2); cursor: pointer; min-height: var(--hit); }
.check input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.check__box { width: 20px; height: 20px; border: 1.5px solid var(--line-strong); border-radius: var(--r-xs); display: grid; place-items: center; background: var(--surface); transition: background-color var(--dur-base) var(--ease), border-color var(--dur-base) var(--ease), transform var(--dur-fast) var(--ease); }
.check__box svg { width: 14px; height: 14px; stroke: var(--on-primary); stroke-width: 2.4; fill: none; stroke-dasharray: 20; stroke-dashoffset: 20; transition: stroke-dashoffset var(--dur-slow) var(--ease); }
.check input:checked + .check__box { background: var(--primary); border-color: var(--primary); }
.check input:checked + .check__box svg { stroke-dashoffset: 0; }
.check:active .check__box { transform: scale(.9); }
.check input:focus-visible + .check__box { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Interruptor */
.switch { display: inline-flex; align-items: center; gap: var(--s-3); cursor: pointer; min-height: var(--hit); }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch__track { width: 36px; height: 20px; border-radius: var(--r-pill); background: var(--line-strong); position: relative; transition: background-color var(--dur-base) var(--ease); flex: none; }
.switch__track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--surface); box-shadow: var(--shadow-1); transition: transform var(--dur-base) var(--ease); }
.switch input:checked + .switch__track { background: var(--primary); }
.switch input:checked + .switch__track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch__track { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- Chips de filtro (interativos; metadado estático não vira chip) ---------- */
.chips { display: flex; gap: var(--s-2); flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: var(--s-1); min-height: var(--control-h-sm); padding: 0 var(--s-3);
  border: 1px solid var(--line); border-radius: var(--r-pill); background: var(--surface); color: var(--ink-2);
  font-size: var(--text-sm); font-weight: 500; transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.chip:hover { border-color: var(--line-strong); color: var(--ink); }
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.chip__remove { display: inline-grid; place-items: center; width: 18px; height: 18px; margin-right: -6px; border-radius: 50%; }
.chip__remove svg { width: 12px; height: 12px; }

/* Controle segmentado (período, horizonte 30/60/90) */
.segmented { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-sunk); border-radius: var(--r-sm); }
.segmented button { border: 0; background: transparent; padding: 0 var(--s-3); min-height: 30px; border-radius: 4px; font-size: var(--text-sm); font-weight: 550; color: var(--ink-2); transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-1); }

/* ---------- Superfícies ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: var(--s-6); }
.panel--flush { padding: 0; overflow: hidden; }
.panel--quiet { background: transparent; border-color: var(--line); }
.panel--sunk { background: var(--surface-sunk); border-color: transparent; }
.panel__head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s-4); margin-bottom: var(--s-4); }
.panel__title { font-size: var(--text-sm); font-weight: 600; margin: 0; }
.panel__meta { font-size: var(--text-xs); color: var(--ink-2); }

/* Estatística: rótulo + número; delta com texto e sinal, nunca só cor */
.stat__label { font-size: var(--text-sm); color: var(--ink-2); margin: 0 0 var(--s-1); }
.stat__value { font-size: var(--text-xl); font-weight: 500; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.1; }
.stat__delta { font-size: var(--text-xs); color: var(--ink-2); margin-top: var(--s-1); }
.stat__delta--pos { color: var(--pos); }
.stat__delta--neg { color: var(--neg-ink); }

/* Selo de estado: ícone + texto, sem pílula decorativa */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: var(--text-xs); font-weight: 550; color: var(--ink-2); }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.status--ok { color: var(--pos); }
.status--warn { color: var(--warn-ink); }
.status--error { color: var(--neg-ink); }

/* Barra de progresso (orçamento: 80% aviso, 100% excedido) */
.progress { height: 6px; border-radius: var(--r-pill); background: var(--surface-sunk); overflow: hidden; }
.progress__bar { height: 100%; width: 100%; border-radius: inherit; background: var(--primary); transform-origin: left; transform: scaleX(var(--value, 0)); transition: transform var(--dur-slow) var(--ease); }
.progress--warn .progress__bar { background: var(--warn); }
.progress--over .progress__bar { background: var(--neg); }

/* ---------- Tabela densa ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.table th { text-align: left; font-size: var(--text-xs); font-weight: 600; color: var(--ink-2); padding: var(--s-2) var(--s-4); border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 0 var(--s-4); height: 48px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
.table th.num, .table td.num { text-align: right; }
.table tbody tr { transition: background-color var(--dur-fast) var(--ease); }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr[aria-selected="true"] { background: var(--primary-bg); }
.table__group th { background: var(--paper); color: var(--ink); font-size: var(--text-xs); padding-top: var(--s-4); position: sticky; top: 0; z-index: 1; }
.table__group .num { color: var(--ink-2); font-weight: 500; }
.table__desc { font-weight: 500; color: var(--ink); }
.table__sub { display: block; font-size: var(--text-xs); color: var(--ink-2); }
.table__actions { display: flex; gap: 2px; justify-content: flex-end; opacity: 0; transition: opacity var(--dur-fast) var(--ease); }
.table tbody tr:hover .table__actions, .table tbody tr:focus-within .table__actions { opacity: 1; }
@media (hover: none) { .table__actions { opacity: 1; } }

/* Abaixo de 640px a linha vira cartão de duas linhas, sem rolagem lateral */
/* Células marcadas com data-col="desc|status|amount|actions|select" */
@media (max-width: 640px) {
  .table--stack, .table--stack tbody { display: block; width: 100%; }
  .table--stack thead { display: none; }
  .table--stack tbody tr { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: var(--s-1) var(--s-3); padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--line-soft); }
  .table--stack td { height: auto; padding: 0; border: 0; min-width: 0; }
  .table--stack td[data-col="select"] { display: none; }
  .table--stack td[data-col="desc"] { grid-area: 1 / 1; }
  .table--stack td[data-col="amount"] { grid-area: 1 / 2; }
  .table--stack td[data-col="status"] { grid-area: 2 / 1; }
  .table--stack td[data-col="actions"] { grid-area: 2 / 2; }
  .table--stack .table__desc, .table--stack .table__sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .table--stack tr.table__group { display: flex; justify-content: space-between; padding: var(--s-4) var(--s-4) var(--s-2); background: var(--paper); }
  .table--stack tr.table__group > * { padding: 0; position: static; background: none; border: 0; }
  .table--stack tr.table__group > :empty { display: none; }
}

/* ---------- Diálogo e gaveta (sobre <dialog>) ---------- */
.dialog { border: 1px solid var(--line); border-radius: var(--r-lg); padding: 0; width: min(560px, calc(100% - 32px)); max-height: calc(100dvh - 64px); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-3); }
.dialog::backdrop { background: var(--scrim); }
.dialog[open] { animation: dialog-in var(--dur-base) var(--ease); }
.dialog[open]::backdrop { animation: fade-in var(--dur-base) var(--ease); }
.dialog__head { display: flex; align-items: center; justify-content: space-between; gap: var(--s-4); padding: var(--s-5) var(--s-6) 0; }
.dialog__title { font-size: var(--text-md); font-weight: 600; }
.dialog__body { padding: var(--s-4) var(--s-6); overflow: auto; }
.dialog__foot { display: flex; justify-content: flex-end; gap: var(--s-2); padding: var(--s-4) var(--s-6) var(--s-5); border-top: 1px solid var(--line-soft); }
.drawer { margin: 0 0 0 auto; height: 100dvh; max-height: 100dvh; width: min(440px, 100%); border-radius: 0; border-width: 0 0 0 1px; flex-direction: column; } /* display só quando aberta: não sobrescrever o display:none do <dialog> fechado */
.drawer[open] { display: flex; animation: drawer-in var(--dur-slow) var(--ease); }
.drawer .dialog__body { flex: 1; }
@media (max-width: 640px) {
  .dialog:not(.drawer) { margin: auto 0 0; width: 100%; max-width: 100%; border-radius: var(--r-lg) var(--r-lg) 0 0; }
  .dialog:not(.drawer)[open] { animation: sheet-in var(--dur-slow) var(--ease); }
}
@keyframes fade-in { from { opacity: 0; } }
@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(.985); } }
@keyframes drawer-in { from { transform: translateX(24px); opacity: 0; } }
@keyframes sheet-in { from { transform: translateY(32px); opacity: 0; } }

/* ---------- Toast com desfazer ---------- */
.toasts { position: fixed; z-index: var(--z-toast); bottom: var(--s-6); left: 50%; transform: translateX(-50%); display: grid; gap: var(--s-2); width: min(420px, calc(100% - 32px)); pointer-events: none; }
.toast { pointer-events: auto; display: flex; align-items: center; gap: var(--s-3); padding: var(--s-3) var(--s-3) var(--s-3) var(--s-4); background: var(--ink); color: var(--paper); border-radius: var(--r); box-shadow: var(--shadow-3); font-size: var(--text-sm); animation: toast-in var(--dur-base) var(--ease); }
.toast__msg { flex: 1; }
.toast__action { border: 0; background: transparent; color: var(--paper); font-weight: 650; padding: var(--s-2) var(--s-3); border-radius: var(--r-sm); text-decoration: underline; text-underline-offset: .2em; }
.toast__action:hover { background: rgb(255 255 255 / .1); }
.toast.is-leaving { opacity: 0; transform: translateY(8px); transition: opacity var(--dur-base) var(--ease), transform var(--dur-base) var(--ease); }
.toast--error { background: var(--neg-ink); }
:root[data-theme="dark"] .toast__action:hover { background: rgb(0 0 0 / .08); }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px); } }

/* ---------- Skeleton ---------- */
.skeleton { display: block; background: var(--surface-sunk); border-radius: var(--r-xs); position: relative; overflow: hidden; }
.skeleton::after { content: ""; position: absolute; inset: 0; transform: translateX(-100%); background: linear-gradient(90deg, transparent, rgb(255 255 255 / .45), transparent); animation: shimmer 1.4s var(--ease) infinite; }
:root[data-theme="dark"] .skeleton::after { background: linear-gradient(90deg, transparent, rgb(255 255 255 / .05), transparent); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .skeleton::after { background: linear-gradient(90deg, transparent, rgb(255 255 255 / .05), transparent); } }
.skeleton--text { height: .9em; margin: .3em 0; }
.skeleton--title { height: 1.6em; width: 45%; }
.skeleton--num { height: 2.4em; width: 60%; }
.skeleton--row { height: 48px; border-radius: 0; border-bottom: 1px solid var(--paper); }
@keyframes shimmer { to { transform: translateX(100%); } }
@media (prefers-reduced-motion: reduce) { .skeleton::after { display: none; } }

/* ---------- Estado vazio e erro ---------- */
.empty { display: grid; justify-items: start; gap: var(--s-2); padding: var(--s-10) var(--s-6); max-width: 46ch; }
.empty--center { justify-items: center; text-align: center; margin-inline: auto; }
.empty__icon { width: 24px; height: 24px; color: var(--ink-3); margin-bottom: var(--s-2); }
.empty__title { font-size: var(--text-md); font-weight: 600; margin: 0; }
.empty__text { color: var(--ink-2); margin: 0 0 var(--s-3); }
.alert { display: flex; gap: var(--s-3); align-items: flex-start; padding: var(--s-3) var(--s-4); border-radius: var(--r-sm); border: 1px solid var(--line); background: var(--surface); font-size: var(--text-sm); }
.alert svg { width: 18px; height: 18px; flex: none; margin-top: 1px; }
.alert__body { flex: 1; }
.alert__title { font-weight: 600; margin: 0 0 2px; }
.alert--error { background: var(--neg-bg); border-color: transparent; }
.alert--error svg { color: var(--neg-ink); }
.alert--warn { background: var(--warn-bg); border-color: transparent; }
.alert--warn svg { color: var(--warn-ink); }
.alert--ok { background: var(--pos-bg); border-color: transparent; }
.alert--ok svg { color: var(--pos); }

/* ---------- Tooltip e popover ---------- */
.tooltip { position: fixed; z-index: var(--z-tooltip); max-width: 260px; padding: 6px var(--s-2); border-radius: var(--r-xs); background: var(--ink); color: var(--paper); font-size: var(--text-xs); line-height: 1.4; pointer-events: none; opacity: 0; transform: translateY(2px); transition: opacity var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease); }
.tooltip.is-visible { opacity: 1; transform: none; }
.popover { position: fixed; z-index: var(--z-overlay); margin: 0; inset: auto; width: min(340px, calc(100vw - 32px)); padding: var(--s-4); border: 1px solid var(--line); border-radius: var(--r); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-2); }
.popover:popover-open { animation: dialog-in var(--dur-base) var(--ease); }

/* Linha de cálculo ("como chegamos neste número") */
.calc { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.calc td { padding: 6px 0; border-bottom: 1px solid var(--line-soft); }
.calc td:last-child { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.calc tr.calc__total td { border-bottom: 0; border-top: 1px solid var(--ink); font-weight: 650; padding-top: var(--s-2); }

/* ---------- Abas ---------- */
.tabs { display: flex; gap: var(--s-6); border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs [role="tab"] { border: 0; background: none; padding: var(--s-3) 0; font-size: var(--text-sm); font-weight: 550; color: var(--ink-2); position: relative; white-space: nowrap; }
.tabs [role="tab"][aria-selected="true"] { color: var(--ink); }
.tabs [role="tab"][aria-selected="true"]::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: var(--ink); }

/* Atalho de teclado */
kbd { font-family: var(--font-mono); font-size: .8em; padding: 1px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: var(--r-xs); background: var(--surface); color: var(--ink-2); }
