@media (max-width: 640px) {
  header .user-area {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: 100%;
  }
  .theme-toggle-btn {
    order: 2;
    margin: 0 12px !important;
    left: 0;
    right: 0;
    position: relative;
    z-index: 30;
  }
  header .user-area > *:not(.theme-toggle-btn) {
    order: 1;
  }
  header .user-area > *:last-child:not(.theme-toggle-btn) {
    order: 3;
  }
}
.theme-toggle-btn {
  border: 1px solid var(--line, rgba(18, 33, 39, 0.1));
  background: var(--card, #ffffff);
  color: var(--ink, #122127);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 0.82rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: transform 0.18s ease, box-shadow 0.18s ease, background 0.2s ease;
}

.theme-toggle-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(8, 37, 44, 0.12);
}

.theme-toggle-btn .theme-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.theme-toggle-btn .theme-icon svg {
  width: 16px;
  height: 16px;
}

@media (max-width: 768px) {
  .theme-toggle-btn {
    min-height: 0;
    padding: 6px 10px;
    justify-content: center;
  }
}

@media (max-width: 560px) {
  .theme-toggle-btn {
    min-height: 0;
    padding: 5px 9px;
    font-size: 0.76rem;
    gap: 5px;
  }

  .theme-toggle-btn .theme-icon svg {
    width: 15px;
    height: 15px;
  }
}

body.dark-mode {
  --bg: #0e171b;
  --ink: #e6f0f2;
  --muted: #9ab0b5;
  --line: rgba(214, 241, 247, 0.16);
  --card: #132127;
  --brand: #13a496;
  --brand-strong: #77d9cc;
  --danger: #ff9990;
  background:
    radial-gradient(circle at 8% 5%, rgba(19, 164, 150, 0.2), transparent 30%),
    radial-gradient(circle at 95% 10%, rgba(238, 127, 59, 0.18), transparent 28%),
    linear-gradient(180deg, #0b1318 0%, var(--bg) 72%);
  color: var(--ink);
}

body.dark-mode header {
  background: rgba(9, 18, 23, 0.82) !important;
  border-bottom-color: var(--line) !important;
}

body.dark-mode .theme-toggle-btn {
  background: rgba(19, 164, 150, 0.12);
  border-color: rgba(119, 217, 204, 0.35);
  color: #dff7f3;
}

body.dark-mode .theme-toggle-btn:hover {
  box-shadow: 0 10px 24px rgba(2, 8, 10, 0.45);
}

body.dark-mode .user-chip,
body.dark-mode .user-pill,
body.dark-mode .btn-logout,
body.dark-mode .card,
body.dark-mode .hero,
body.dark-mode .kpi,
body.dark-mode .kpi-card,
body.dark-mode .table-card,
body.dark-mode .plan-card,
body.dark-mode .summary,
body.dark-mode .user-menu-panel,
body.dark-mode .modal-content,
body.dark-mode .panel,
body.dark-mode .empty-state,
body.dark-mode .report,
body.dark-mode .insight-card {
  background: var(--card) !important;
  border-color: var(--line) !important;
  color: var(--ink) !important;
}

body.dark-mode .btn-logout {
  background: rgba(192, 57, 43, 0.18) !important;
  border-color: rgba(192, 57, 43, 0.55) !important;
  color: #f08070 !important;
}

body.dark-mode .btn-logout:hover {
  background: rgba(192, 57, 43, 0.28) !important;
  border-color: rgba(192, 57, 43, 0.85) !important;
}

body.dark-mode .sub-item,
body.dark-mode .pill,
body.dark-mode .badge,
body.dark-mode .chip,
body.dark-mode .period-btn,
body.dark-mode .btn-soft {
  border-color: var(--line) !important;
}

body.dark-mode nav a {
  color: var(--muted) !important;
}

body.dark-mode nav a:hover,
body.dark-mode nav a.active {
  background: rgba(19, 164, 150, 0.18) !important;
  color: #bdf5ed !important;
}

body.dark-mode input,
body.dark-mode select,
body.dark-mode textarea {
  background: #0f1b21 !important;
  color: var(--ink) !important;
  border-color: var(--line) !important;
}

body.dark-mode table,
body.dark-mode th,
body.dark-mode td {
  border-color: var(--line) !important;
}

body.dark-mode thead th {
  background: rgba(19, 33, 40, 0.95) !important;
  color: var(--ink) !important;
}

body.dark-mode tbody tr:hover {
  background: rgba(19, 164, 150, 0.08) !important;
}

body.dark-mode .muted,
body.dark-mode p,
body.dark-mode .kpi-sub,
body.dark-mode .subscription-label,
body.dark-mode .sub-item span {
  color: var(--muted) !important;
}

/* ===== DARK MODE - CORES HARDCODED ===== */

/* Cabeçalho da tabela */
body.dark-mode thead th,
body.dark-mode .table-wrap thead th {
  background: #1a2f38 !important;
  color: var(--ink) !important;
}

/* Nomes e labels com cor fixa */
body.dark-mode .td-nome strong,
body.dark-mode .sub-item strong,
body.dark-mode .modal h2,
body.dark-mode .page-head h1,
body.dark-mode .kpi-value {
  color: var(--ink) !important;
}

body.dark-mode .field label {
  color: #c9dfe4 !important;
}

body.dark-mode .modal .sub,
body.dark-mode .page-head p {
  color: var(--muted) !important;
}

/* Inputs e selects dentro de modais/cards */
body.dark-mode .field input,
body.dark-mode .field select,
body.dark-mode .field-preco-readonly {
  background: #0f1b21 !important;
  color: var(--ink) !important;
  border-color: var(--line) !important;
}

/* Badges de status */
body.dark-mode .badge-ok {
  color: #77d9cc !important;
  background: rgba(19, 164, 150, 0.2) !important;
}

body.dark-mode .badge-low {
  color: #f4a96a !important;
  background: rgba(238, 127, 59, 0.22) !important;
}

body.dark-mode .badge-zero {
  color: #ff9990 !important;
  background: rgba(255, 100, 80, 0.18) !important;
}

body.dark-mode .badge-cat-final {
  color: #90b8ff !important;
  background: rgba(51, 119, 255, 0.2) !important;
  border-color: rgba(51, 119, 255, 0.35) !important;
}

body.dark-mode .badge-cat-raw {
  color: #77d9cc !important;
  background: rgba(19, 164, 150, 0.2) !important;
  border-color: rgba(19, 164, 150, 0.35) !important;
}

/* KPI sub coloridos */
body.dark-mode .kpi-sub.ok { color: #77d9cc !important; }
body.dark-mode .kpi-sub.danger { color: #ff9990 !important; }

/* Botões */
body.dark-mode .btn-soft {
  background: rgba(19, 164, 150, 0.15) !important;
  color: var(--brand-strong) !important;
  border-color: rgba(119, 217, 204, 0.3) !important;
}

body.dark-mode .btn-danger {
  background: rgba(255, 100, 80, 0.15) !important;
  color: #ff9990 !important;
  border-color: rgba(255, 100, 80, 0.3) !important;
}

/* Botões +/- de quantidade */
body.dark-mode .qty-btn {
  background: rgba(19, 164, 150, 0.08) !important;
  border-color: var(--line) !important;
  color: var(--muted) !important;
}

body.dark-mode .qty-btn:hover {
  background: rgba(19, 164, 150, 0.22) !important;
  color: var(--brand-strong) !important;
}

/* Status de assinatura */
body.dark-mode .subscription-status {
  background: rgba(238, 127, 59, 0.22) !important;
  color: #f4a96a !important;
}

body.dark-mode .subscription-status.is-active {
  background: rgba(19, 164, 150, 0.22) !important;
  color: #77d9cc !important;
}

/* Filtro de categorias ativo */
body.dark-mode .filter-chip {
  background: var(--card) !important;
  color: var(--muted) !important;
  border-color: var(--line) !important;
}

body.dark-mode .filter-chip.active {
  background: rgba(19, 164, 150, 0.2) !important;
  border-color: rgba(119, 217, 204, 0.4) !important;
  color: var(--brand-strong) !important;
}

/* Feedback de erro no modal */
body.dark-mode .modal-feedback.error {
  color: #f4a96a !important;
  background: rgba(238, 127, 59, 0.18) !important;
  border-color: rgba(238, 127, 59, 0.38) !important;
}

/* User chip */
body.dark-mode .user-chip {
  color: var(--ink) !important;
  background: var(--card) !important;
  border-color: var(--line) !important;
}

/* Skeleton loader */
body.dark-mode .skeleton {
  background: linear-gradient(90deg,
    rgba(214,241,247,0.06) 25%,
    rgba(214,241,247,0.12) 50%,
    rgba(214,241,247,0.06) 75%) !important;
  background-size: 200% !important;
}

/* ===== RELATÓRIOS - INSIGHT CARDS ===== */

body.dark-mode .insight-card {
  background: var(--card) !important;
  border-color: var(--line) !important;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3) !important;
}

body.dark-mode .insight-card.is-highlight {
  background: rgba(19, 33, 40, 0.9) !important;
}

body.dark-mode .insight-card .insight-label {
  color: var(--muted) !important;
}

body.dark-mode .insight-card .insight-value {
  color: var(--ink) !important;
}

body.dark-mode .insight-card .insight-note {
  color: var(--muted) !important;
}

body.dark-mode .insight-card .insight-product {
  background: rgba(19, 164, 150, 0.2) !important;
  border-color: rgba(19, 164, 150, 0.35) !important;
  color: #77d9cc !important;
}

body.dark-mode .insight-card.is-critical .insight-product {
  background: rgba(255, 100, 80, 0.18) !important;
  border-color: rgba(255, 100, 80, 0.35) !important;
  color: #ff9990 !important;
}

/* Tabela de relatórios */
body.dark-mode .table-wrap {
  background: var(--card) !important;
  border-color: var(--line) !important;
  box-shadow: none !important;
}

body.dark-mode tbody tr:nth-child(even) td {
  background: rgba(19, 164, 150, 0.04) !important;
}

body.dark-mode .item-name {
  color: var(--ink) !important;
}

body.dark-mode .metric-value {
  color: var(--ink) !important;
}

body.dark-mode .metric-alert {
  color: #ff9990 !important;
}

body.dark-mode .metric-suggested {
  color: var(--brand-strong) !important;
  background: rgba(19, 164, 150, 0.15) !important;
}

body.dark-mode .buy-when {
  color: var(--ink) !important;
}

body.dark-mode .rec-title {
  color: var(--muted) !important;
}

body.dark-mode .rec-action {
  background: rgba(19, 164, 150, 0.12) !important;
  color: var(--ink) !important;
}

body.dark-mode .rec-action strong,
body.dark-mode .rec-action span {
  color: var(--brand-strong) !important;
  opacity: 1 !important;
}

body.dark-mode .rec-summary {
  color: var(--ink) !important;
}

body.dark-mode .recommendation-meta {
  color: var(--muted) !important;
}

body.dark-mode .recommendation-meta strong {
  color: var(--ink) !important;
}

body.dark-mode .empty-report {
  background: rgba(19, 33, 40, 0.6) !important;
  border-color: var(--line) !important;
  color: var(--muted) !important;
}

/* Barras de progresso */
body.dark-mode .bar {
  background: rgba(214, 241, 247, 0.1) !important;
}

/* Badges de relatório */
body.dark-mode .badge-up {
  color: #77d9cc !important;
  background: rgba(19, 164, 150, 0.2) !important;
}

body.dark-mode .badge-down,
body.dark-mode .badge-warn {
  color: #f4a96a !important;
  background: rgba(238, 127, 59, 0.2) !important;
}

body.dark-mode .badge-critical {
  color: #ff9990 !important;
  background: rgba(255, 100, 80, 0.18) !important;
}

/* Painel de ranking */
body.dark-mode .full-panel,
body.dark-mode .panel {
  background: var(--card) !important;
  border-color: var(--line) !important;
}

body.dark-mode .full-panel h2,
body.dark-mode .panel h2 {
  color: var(--ink) !important;
}

body.dark-mode .full-panel > p,
body.dark-mode .panel > p {
  color: var(--muted) !important;
}

body.dark-mode .period-filters {
  background: var(--card) !important;
  border-color: var(--line) !important;
}

body.dark-mode .period-filters span {
  color: var(--muted) !important;
}

body.dark-mode .period-btn {
  background: rgba(19, 164, 150, 0.1) !important;
  color: var(--brand-strong) !important;
  border-color: rgba(119, 217, 204, 0.3) !important;
}

body.dark-mode .period-btn.active {
  background: linear-gradient(135deg, var(--brand), #0fb8a9) !important;
  color: #fff !important;
  border-color: transparent !important;
}

/* ===== INSUMOS - MODAL DE PRODUÇÃO ===== */

body.dark-mode .prod-component-row {
  background: rgba(19, 33, 40, 0.8) !important;
  border-color: var(--line) !important;
}

body.dark-mode .prod-component-row .prod-comp-select,
body.dark-mode .prod-component-row .prod-comp-qty,
body.dark-mode .prod-component-row .prod-extra-cost-label,
body.dark-mode .prod-component-row .prod-extra-cost-value {
  background: #0f1b21 !important;
  color: var(--ink) !important;
  border-color: var(--line) !important;
}

body.dark-mode .prod-component-price {
  color: var(--muted) !important;
}

body.dark-mode .prod-component-price strong {
  color: var(--ink) !important;
}

body.dark-mode .production-summary {
  background: rgba(19, 164, 150, 0.1) !important;
  border-color: rgba(119, 217, 204, 0.25) !important;
  color: var(--brand-strong) !important;
}

body.dark-mode .prod-summary-item {
  background: rgba(19, 33, 40, 0.9) !important;
  border-color: rgba(119, 217, 204, 0.15) !important;
}

body.dark-mode .prod-summary-label {
  color: var(--muted) !important;
}

body.dark-mode .prod-summary-value {
  color: var(--ink) !important;
}

body.dark-mode .prod-summary-item-total {
  background: rgba(19, 164, 150, 0.15) !important;
}

body.dark-mode .production-summary.is-profit-positive {
  border-color: rgba(11, 200, 110, 0.3) !important;
  background: rgba(11, 200, 110, 0.08) !important;
  color: #5de8a0 !important;
}

body.dark-mode .production-summary.is-profit-negative {
  border-color: rgba(255, 100, 80, 0.3) !important;
  background: rgba(255, 100, 80, 0.08) !important;
  color: #ff9990 !important;
}

body.dark-mode .production-summary.is-profit-neutral {
  border-color: var(--line) !important;
  background: rgba(214, 241, 247, 0.05) !important;
  color: var(--muted) !important;
}

body.dark-mode .prod-profit-positive .prod-summary-label,
body.dark-mode .prod-profit-positive .prod-summary-value {
  color: #5de8a0 !important;
}

body.dark-mode .prod-profit-negative .prod-summary-label,
body.dark-mode .prod-profit-negative .prod-summary-value {
  color: #ff9990 !important;
}

body.dark-mode .prod-profit-neutral .prod-summary-label,
body.dark-mode .prod-profit-neutral .prod-summary-value {
  color: var(--muted) !important;
}

body.dark-mode #labelQtdMeta {
  color: var(--brand-strong) !important;
}

/* ===== INSUMOS - NOMES E IDs DA TABELA ===== */

body.dark-mode .material-name {
  color: var(--ink) !important;
}

body.dark-mode .material-id {
  color: var(--muted) !important;
  opacity: 1 !important;
}

/* Badges de insumos */
body.dark-mode .badge-ok {
  color: #77d9cc !important;
  background: rgba(19, 164, 150, 0.2) !important;
}

body.dark-mode .badge-low {
  color: #f4a96a !important;
  background: rgba(238, 127, 59, 0.22) !important;
}

body.dark-mode .badge-zero {
  color: #ff9990 !important;
  background: rgba(255, 100, 80, 0.18) !important;
}

/* ===== OVERRIDES GLOBAIS PARA TODAS AS PÁGINAS ===== */

/* Cabeçalhos de tabela com fundo fixo claro */
body.dark-mode thead th {
  background: #1a2f38 !important;
  color: #c9dfe4 !important;
}

/* Textos com cores escuras hardcoded comuns */
body.dark-mode .field label,
body.dark-mode label {
  color: #c9dfe4 !important;
}

/* Campos de formulário com fundo claro fixo */
body.dark-mode input:not([type="checkbox"]):not([type="radio"]),
body.dark-mode select,
body.dark-mode textarea {
  background: #0f1b21 !important;
  color: var(--ink) !important;
  border-color: var(--line) !important;
}

/* Textos que usam cores hardcoded escuras (#16353c, #194149, #1f3840, #173a42) */
body.dark-mode strong,
body.dark-mode b {
  color: inherit;
}

body.dark-mode .sub-item strong,
body.dark-mode .td-nome strong,
body.dark-mode .cell-bold {
  color: var(--ink) !important;
}

/* Datalist/autocomplete */
body.dark-mode datalist {
  background: #0f1b21 !important;
  color: var(--ink) !important;
}

/* Placeholders */
body.dark-mode ::placeholder {
  color: var(--muted) !important;
  opacity: 0.7;
}

/* Scrollbar em dark mode */
body.dark-mode ::-webkit-scrollbar {
  background: #0e171b;
}

body.dark-mode ::-webkit-scrollbar-thumb {
  background: rgba(119, 217, 204, 0.2);
  border-radius: 99px;
}

body.dark-mode ::-webkit-scrollbar-thumb:hover {
  background: rgba(119, 217, 204, 0.35);
}

body.dark-mode ::-webkit-scrollbar-corner {
  background: #0e171b;
}

/* Cards de insight/relatório */
body.dark-mode .insight-card,
body.dark-mode .report-card,
body.dark-mode .section-card,
body.dark-mode .filter-bar {
  background: var(--card) !important;
  border-color: var(--line) !important;
  color: var(--ink) !important;
}

/* Subtextos dentro de cards */
body.dark-mode .kpi-label,
body.dark-mode .card-label,
body.dark-mode .section-label {
  color: var(--muted) !important;
}

/* Links de navegação */
body.dark-mode a:not(.btn):not(.brand) {
  color: var(--muted);
}

body.dark-mode a:not(.btn):not(.brand):hover {
  color: var(--brand-strong);
}

/* Divisores e separadores */
body.dark-mode hr {
  border-color: var(--line) !important;
}

/* Overlay do modal */
body.dark-mode .overlay {
  background: rgba(5, 12, 16, 0.65) !important;
}

/* Period buttons (relatórios) */
body.dark-mode .period-btn {
  background: var(--card) !important;
  color: var(--muted) !important;
  border-color: var(--line) !important;
}

body.dark-mode .period-btn.active,
body.dark-mode .period-btn:hover {
  background: rgba(19, 164, 150, 0.2) !important;
  color: var(--brand-strong) !important;
  border-color: rgba(119, 217, 204, 0.4) !important;
}

/* ===== CAMADA DE SEGURANCA DE TEMA (CLARO/ESCURO) ===== */

/* Modo claro: garante legibilidade em controles nativos */
body:not(.dark-mode) input:not([type="checkbox"]):not([type="radio"]),
body:not(.dark-mode) select,
body:not(.dark-mode) textarea {
  color: #17343f;
  background: #fafcfc;
  border-color: rgba(18, 33, 39, 0.18);
}

body:not(.dark-mode) select option,
body:not(.dark-mode) select optgroup {
  color: #17343f;
  background: #f8fbfc;
}

/* Modo escuro: evita texto/fundo invertido em dropdowns e listas */
body.dark-mode select option,
body.dark-mode select optgroup {
  color: var(--ink) !important;
  background: #0f1b21 !important;
}

/* Lista de materiais da venda no dashboard */
body.dark-mode #saleMaterial {
  color: var(--ink) !important;
  background: #0f1b21 !important;
  border-color: var(--line) !important;
}

body.dark-mode #saleMaterial option {
  color: var(--ink) !important;
  background: #0f1b21 !important;
}

body.dark-mode #saleMaterial option:checked {
  color: #ffffff !important;
  background: linear-gradient(135deg, #1d67c6, #2c7be5) !important;
}

/* Pré-visualização de item de venda com contraste consistente */
body.dark-mode .sale-material-preview {
  color: #eaf6fb !important;
  border-color: rgba(126, 222, 235, 0.3) !important;
  background: linear-gradient(145deg, rgba(12, 52, 71, 0.88), rgba(8, 32, 43, 0.92)) !important;
}

body.dark-mode .sale-material-preview.is-empty {
  color: #9fb8c2 !important;
  background: rgba(9, 35, 49, 0.55) !important;
}
