/**
 * Fase 19 — Design system e polimento UX/UI
 * Camada sobre styles.css (não substitui classes existentes).
 */

/* ========== 1. Tokens ========== */
:root {
  --color-primary: #1d4ed8;
  --color-primary-hover: #1e40af;
  --color-primary-soft: #dbeafe;

  --color-bg: #f8fafc;
  --color-surface: #ffffff;
  --color-surface-soft: #f1f5f9;

  --color-text: #0f172a;
  --color-muted: #64748b;
  --color-border: #e2e8f0;

  --color-success: #16a34a;
  --color-warning: #d97706;
  --color-danger: #dc2626;
  --color-info: #0284c7;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 20px;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 18px 40px rgba(15, 23, 42, 0.12);

  --transition-fast: 160ms ease;

  /* Aliases legados */
  --bg: var(--color-bg);
  --surface: var(--color-surface);
  --text: var(--color-text);
  --text-muted: var(--color-muted);
  --primary: var(--color-primary);
  --primary-hover: var(--color-primary-hover);
  --border: var(--color-border);
  --shadow: var(--shadow-md);
  --radius: var(--radius-lg);
}

/* ========== 2. Base ========== */
html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ========== 3. Layout ========== */
.app-shell {
  background: var(--color-bg);
}

.main {
  background: var(--color-bg);
}

.content {
  padding: 1rem 1rem 1.5rem;
}

.page-header {
  margin-bottom: 1.25rem;
}

.page-header h2 {
  margin: 0 0 0.25rem;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.page-header p,
.page-header .page-header__subtitle {
  margin: 0;
  color: var(--color-muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

/* ========== 4. Sidebar ========== */
.sidebar {
  box-shadow: var(--shadow-sm);
}

.nav-link {
  transition: background var(--transition-fast), color var(--transition-fast);
}

.nav-link:focus-visible {
  outline-offset: -2px;
}

.nav-link.active {
  font-weight: 600;
  box-shadow: inset 3px 0 0 var(--color-primary);
}

/* ========== 5. Topbar ========== */
.topbar {
  box-shadow: var(--shadow-sm);
}

.topbar__title h1 {
  font-weight: 700;
  letter-spacing: -0.02em;
}

.user-chip {
  display: inline-flex;
  align-items: center;
}

@media (min-width: 768px) {
  .user-chip {
    display: inline-flex;
  }
}

/* ========== 6. Cards ========== */
.card,
.panel,
.stat-card,
.chart-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

.card-header,
.panel__header {
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 0.75rem;
}

.metric-card {
  padding: 1rem 1.15rem;
}

/* ========== 7. Botões ========== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  font-family: inherit;
  line-height: 1.2;
  transition:
    background var(--transition-fast),
    color var(--transition-fast),
    border-color var(--transition-fast),
    transform 0.1s ease;
}

.btn:disabled,
.btn[aria-busy='true'] {
  opacity: 0.65;
  cursor: not-allowed;
  transform: none;
}

.btn--primary,
.btn-primary {
  background: var(--color-primary);
  color: #fff;
}

.btn--primary:hover:not(:disabled),
.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
}

.btn--secondary,
.btn-secondary {
  background: var(--color-primary-soft);
  color: var(--color-primary);
}

.btn--danger,
.btn-danger {
  background: #fef2f2;
  color: var(--color-danger);
}

.btn--danger:hover:not(:disabled),
.btn-danger:hover:not(:disabled) {
  background: #fee2e2;
}

.btn--outline,
.btn-outline {
  background: transparent;
  border: 1px solid var(--color-border);
  color: var(--color-text);
}

.btn--outline:hover:not(:disabled) {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.btn--ghost:hover:not(:disabled) {
  background: var(--color-surface-soft);
  color: var(--color-text);
}

.btn--loading {
  position: relative;
  pointer-events: none;
}

.btn--loading::after {
  content: '';
  width: 1em;
  height: 1em;
  margin-left: 0.35rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: pc-spin 0.65s linear infinite;
}

@keyframes pc-spin {
  to {
    transform: rotate(360deg);
  }
}

.btn-icon {
  min-width: 44px;
  padding-left: 0.65rem;
  padding-right: 0.65rem;
}

/* ========== 8. Formulários ========== */
.form-field,
.form-group {
  margin-bottom: 0;
}

.form-field label,
.form-label {
  color: var(--color-text);
}

.form-field input,
.form-field select,
.form-field textarea,
.form-control {
  border-color: var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
}

.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus,
.form-control:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.12);
}

.form-help {
  font-size: 0.75rem;
  color: var(--color-muted);
  margin-top: 0.25rem;
}

.form-error {
  font-size: 0.8rem;
  color: var(--color-danger);
  margin-top: 0.25rem;
}

.form-grid {
  display: grid;
  gap: 1rem;
}

.form-actions,
.modal__actions {
  gap: 0.5rem;
  padding-top: 0.5rem;
}

/* ========== 9. Tabelas ========== */
.table-wrapper,
.entity-table-wrap {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
}

.entity-table,
.data-table {
  width: 100%;
  border-collapse: collapse;
}

.entity-table th {
  background: var(--color-surface-soft);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-muted);
}

.table-actions,
.entity-table .table-actions {
  white-space: nowrap;
}

.cell-mono {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.82rem;
}

.mobile-card-list,
.entity-cards {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.entity-card,
.grupo-card,
.mobile-data-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 1rem;
  box-shadow: var(--shadow-sm);
}

/* ========== 10. Modais ========== */
.modal {
  z-index: 80;
}

.modal__panel {
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}

.modal__header h2 {
  font-size: 1.05rem;
  font-weight: 700;
}

@media (max-width: 767px) {
  .modal__panel {
    max-height: 92dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }

  .modal {
    align-items: flex-end;
    padding: 0;
  }

  .modal__actions {
    flex-direction: column-reverse;
  }

  .modal__actions .btn {
    width: 100%;
    min-height: 48px;
  }
}

/* ========== 11. Badges ========== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1.3;
  white-space: nowrap;
}

.badge--success,
.badge-success {
  background: #dcfce7;
  color: #166534;
}

.badge--warning,
.badge--warn,
.badge-warning {
  background: #fef3c7;
  color: #92400e;
}

.badge--danger,
.badge-danger {
  background: #fee2e2;
  color: #991b1b;
}

.badge--info,
.badge-info {
  background: #e0f2fe;
  color: #075985;
}

.badge--muted,
.badge-muted {
  background: var(--color-surface-soft);
  color: var(--color-muted);
}

/* ========== 12. Toasts / alertas ========== */
.toast {
  top: auto;
  bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 1rem);
  border-radius: var(--radius-md);
  padding: 0.85rem 1.15rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.25s ease;
}

@media (min-width: 768px) {
  .toast {
    bottom: auto;
    top: 1rem;
  }
}

.toast--success {
  background: #166534;
  color: #fff;
}

.toast--error {
  background: #991b1b;
  color: #fff;
}

.toast--warning {
  background: #92400e;
  color: #fff;
}

.toast--info {
  background: #0f172a;
  color: #fff;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

.alert--danger,
.entity-error {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
}

.alert--danger[hidden],
.entity-error[hidden] {
  display: none !important;
}

/* ========== 13. Loading / vazio ========== */
.page-loading,
.entity-loading {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--color-muted);
  font-size: 0.9rem;
}

.skeleton {
  background: linear-gradient(
    90deg,
    var(--color-surface-soft) 25%,
    #e2e8f0 50%,
    var(--color-surface-soft) 75%
  );
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.2s ease-in-out infinite;
  border-radius: var(--radius-sm);
  min-height: 1rem;
}

@keyframes skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

.empty-state {
  text-align: center;
  padding: 2.5rem 1.25rem;
  background: var(--color-surface);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-md);
}

.empty-state__icon {
  font-size: 1.5rem;
  color: var(--color-muted);
  margin-bottom: 0.5rem;
  opacity: 0.5;
}

.empty-state__title {
  margin: 0 0 0.35rem;
  font-size: 1rem;
  font-weight: 600;
}

.empty-state__desc {
  margin: 0 0 1rem;
  color: var(--color-muted);
  font-size: 0.9rem;
  max-width: 28rem;
  margin-left: auto;
  margin-right: auto;
}

.empty-state__actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* ========== 14. Login ========== */
.login-card {
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-md);
}

.login-card__brand .sidebar__logo {
  flex-shrink: 0;
}

/* ========== 15. Filtros ========== */
.filters-bar {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 0.75rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow-sm);
}

.filters-bar__search,
.filters-bar__select {
  min-height: 44px;
}

/* ========== 16. PWA banner ========== */
.pwa-banner {
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}

/* ========== 17. Responsividade fina ========== */
@media (max-width: 359px) {
  .content {
    padding: 0.75rem;
  }

  .topbar__title h1 {
    font-size: 1rem;
  }
}

@media (min-width: 768px) {
  .app-shell {
    display: flex;
  }

  .main {
    margin-left: 0;
    flex: 1;
    min-width: 0;
  }

  .sidebar {
    position: sticky;
    flex-shrink: 0;
  }
}

@media (min-width: 1024px) {
  .content {
    padding: 1.5rem 2rem 2rem;
    max-width: 1280px;
  }
}

@media (min-width: 1366px) {
  .cards-grid--executive {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* Bottom nav polish */
.bottom-nav a {
  transition: color var(--transition-fast);
}

.bottom-nav a.active {
  color: var(--color-primary);
  font-weight: 600;
}
