/* =========================================================================
   Componentes reutilizáveis
   ========================================================================= */

/* ---------- Botões ---------- */
.btn {
  --_bg: var(--surface);
  --_fg: var(--text);
  --_bd: var(--border);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 16px;
  border: 1px solid var(--_bd); border-radius: var(--radius-sm);
  background: var(--_bg); color: var(--_fg);
  font-weight: 550; font-size: .925rem; white-space: nowrap;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .05s;
  text-decoration: none !important;
  user-select: none;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; transform: none; }

.btn-primary { --_bg: var(--primary); --_fg: var(--on-primary); --_bd: var(--primary); box-shadow: var(--shadow-xs); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { --_bg: var(--danger); --_fg: #fff; --_bd: var(--danger); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-ghost { --_bg: transparent; --_bd: transparent; --_fg: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-soft { --_bg: var(--primary-soft); --_fg: var(--primary); --_bd: transparent; }
.btn-soft:hover { background: #e0e7ff; }

.btn-sm { height: 32px; padding: 0 12px; font-size: .85rem; }
.btn-lg { height: 48px; padding: 0 22px; font-size: 1rem; border-radius: var(--radius); }
.btn-block { width: 100%; }

.btn.carregando { position: relative; color: transparent !important; pointer-events: none; }
.btn.carregando::after {
  content: ''; position: absolute; width: 18px; height: 18px;
  border: 2px solid currentColor; border-color: var(--_fg) var(--_fg) transparent transparent;
  border-radius: 50%; animation: girar .7s linear infinite;
}

.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; padding: 0;
  border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--muted);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .35; cursor: not-allowed; background: transparent; }
.icon-btn.sm { width: 30px; height: 30px; }
.icon-btn.perigo:hover { background: var(--danger-soft); color: var(--danger); }

/* ---------- Formulários ---------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, .label { font-size: .85rem; font-weight: 600; color: var(--text-2); }
.field-hint { font-size: .8rem; color: var(--muted); }
.field-error { font-size: .8rem; color: var(--danger); min-height: 0; }
.field-error:empty { display: none; }

.input {
  width: 100%; height: 42px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.input::placeholder { color: #94a3b8; }
.input:hover { border-color: #b8c0d0; }
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px var(--primary-ring); }
textarea.input { height: auto; padding: 10px 12px; resize: vertical; min-height: 96px; line-height: 1.5; }
select.input { padding-right: 32px; appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; }
.input-sm { height: 36px; font-size: .875rem; }
.field.invalido .input, .input.invalido { border-color: var(--danger); }
.field.invalido .input:focus, .input.invalido:focus { box-shadow: 0 0 0 4px rgba(220, 38, 38, .15); }

.input-icon { position: relative; display: block; }
.input-icon > .ic { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.input-icon > .input { padding-left: 38px; }

.input-prefix { display: flex; align-items: stretch; }
.input-prefix > span {
  display: grid; place-items: center; padding: 0 12px;
  border: 1px solid var(--border-strong); border-right: 0;
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  background: var(--surface-2); color: var(--muted); font-size: .875rem; font-weight: 500;
}
.input-prefix > .input { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }

.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 560px) { .row-2 { grid-template-columns: 1fr; } }

/* Switch */
.switch { position: relative; display: inline-flex; flex-shrink: 0; width: 42px; height: 24px; cursor: pointer; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch > span { position: absolute; inset: 0; border-radius: 999px; background: #cbd5e1; transition: background .2s; }
.switch > span::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25);
  transition: transform .2s var(--ease);
}
.switch input:checked + span { background: var(--success); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }
.switch input:disabled + span { opacity: .5; }

.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 16px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-2); cursor: pointer;
}
.switch-row > span:first-child { display: flex; flex-direction: column; gap: 2px; font-size: .9rem; }
.switch-row .muted { font-size: .8rem; }

/* Segmented control */
.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.segmented button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 14px; border: 0; border-radius: 7px; background: transparent;
  color: var(--muted); font-size: .85rem; font-weight: 550; transition: all .15s;
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  height: 22px; padding: 0 8px; border-radius: 999px;
  font-size: .72rem; font-weight: 650; letter-spacing: .01em; white-space: nowrap;
  background: var(--surface-3); color: var(--text-2);
}
.badge-sucesso { background: var(--success-soft); color: var(--success); }
.badge-erro { background: var(--danger-soft); color: var(--danger); }
.badge-aviso { background: var(--warning-soft); color: var(--warning); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-primary { background: var(--primary-soft); color: var(--primary); }
.badge-promo { background: #fee2e2; color: #b91c1c; }
.badge-pro { background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-xs);
}
.card-pad { padding: 20px; }

/* ---------- Progresso ---------- */
.progress { height: 6px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.progress > span { display: block; height: 100%; border-radius: inherit; background: var(--primary); transition: width .4s var(--ease); }
.progress.alerta > span { background: #f59e0b; }
.progress.cheio > span { background: var(--danger); }

/* ---------- Estado vazio ---------- */
.empty {
  display: flex; flex-direction: column; align-items: center; text-align: center; gap: 10px;
  padding: 56px 24px; color: var(--muted);
}
.empty-ic {
  display: grid; place-items: center; width: 64px; height: 64px; margin-bottom: 6px;
  border-radius: 20px; background: var(--primary-soft); color: var(--primary);
}
.empty h3 { color: var(--text); font-size: 1.05rem; }
.empty p { max-width: 380px; }
.empty .btn { margin-top: 8px; }

/* ---------- Loading ---------- */
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--surface-3); border-top-color: var(--primary);
  animation: girar .8s linear infinite;
}
@keyframes girar { to { transform: rotate(360deg); } }

.skeleton {
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: brilho 1.3s ease infinite;
}
@keyframes brilho { from { background-position: 100% 50%; } to { background-position: 0 50%; } }

/* ---------- Modal (<dialog>) ---------- */
dialog.modal {
  padding: 0; border: 0; background: transparent; color: inherit;
  width: min(560px, calc(100vw - 32px)); max-width: none;
  max-height: calc(100dvh - 48px); overflow: visible;
  animation: modal-in .22s var(--ease);
}
dialog.modal-sm { width: min(420px, calc(100vw - 32px)); }
dialog.modal-lg { width: min(980px, calc(100vw - 32px)); }
dialog.modal::backdrop { background: rgba(15, 23, 42, .45); backdrop-filter: blur(3px); animation: fade-in .2s ease; }
dialog.modal.saindo { animation: modal-out .15s ease forwards; }
dialog.modal.saindo::backdrop { animation: fade-out .15s ease forwards; }

.modal-card {
  display: flex; flex-direction: column;
  max-height: calc(100dvh - 48px);
  background: var(--surface); border-radius: var(--radius-xl); box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 18px 20px 14px 24px; border-bottom: 1px solid var(--border);
}
.modal-body { padding: 20px 24px; overflow-y: auto; overscroll-behavior: contain; }
.modal-foot {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 14px 24px; border-top: 1px solid var(--border); background: var(--surface-2);
}
.confirm-msg { color: var(--text-2); }

@media (max-width: 640px) {
  dialog.modal, dialog.modal-lg, dialog.modal-sm {
    width: 100vw; max-height: 100dvh; margin: auto 0 0;
  }
  dialog.modal-lg .modal-card { height: 100dvh; max-height: 100dvh; border-radius: 0; }
  .modal-card { max-height: 92dvh; border-radius: var(--radius-xl) var(--radius-xl) 0 0; }
  .modal-body { padding: 16px; }
  .modal-head { padding: 14px 12px 12px 16px; }
  .modal-foot { padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); }
  .modal-foot .btn { flex: 1; }
}

@keyframes modal-in { from { opacity: 0; transform: translateY(12px) scale(.98); } }
@keyframes modal-out { to { opacity: 0; transform: translateY(8px) scale(.98); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }

/* ---------- Toasts ---------- */
.toasts {
  position: fixed; inset: auto 20px 20px auto; margin: 0; padding: 0;
  border: 0; background: transparent; overflow: visible;
  display: flex; flex-direction: column; gap: 8px; align-items: flex-end;
  width: auto; height: auto; max-width: calc(100vw - 40px);
  z-index: 1000;
}
.toast {
  display: flex; align-items: center; gap: 10px;
  min-width: 240px; max-width: 420px; padding: 12px 16px;
  border-radius: var(--radius); background: #0f172a; color: #fff;
  box-shadow: var(--shadow-lg); font-size: .9rem; font-weight: 500;
  animation: toast-in .25s var(--ease);
}
.toast-sucesso .ic { color: #4ade80; }
.toast-erro .ic { color: #f87171; }
.toast-aviso .ic { color: #fbbf24; }
.toast-info .ic { color: #93c5fd; }
.toast.saindo { animation: toast-out .2s ease forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }
@keyframes toast-out { to { opacity: 0; transform: translateX(12px); } }
@media (max-width: 640px) {
  .toasts { inset: auto 12px calc(12px + env(safe-area-inset-bottom)) 12px; align-items: stretch; max-width: none; }
  .toast { max-width: none; }
}
