/* AVA 13.4.5 · Tema oscuro operativo. La identidad azul se conserva y todos
   los cambios se apoyan en los mismos tokens semánticos del tema claro. */
html[data-theme="dark"] {
  color-scheme: dark;
  --ava-primary: #2B78AD;
  --ava-primary-dark: #081F34;
  --ava-primary-hover: #23658F;
  --ava-interactive: #62B7EE;
  --ava-primary-soft: #17354A;
  --ava-background: #09131E;
  --ava-surface: #111F2D;
  --ava-border: #31465A;
  --ava-text-primary: #F1F5F9;
  --ava-text-secondary: #B7C4D1;
  --ava-text-disabled: #7F91A3;
  --ava-success: #218A67;
  --ava-success-dark: #86DDBD;
  --ava-success-action: #17684E;
  --ava-success-soft: #14372E;
  --ava-warning: #B97013;
  --ava-warning-dark: #FFD08A;
  --ava-warning-soft: #432E13;
  --ava-danger: #C8443B;
  --ava-danger-dark: #FFAAA4;
  --ava-danger-soft: #482321;
  --ava-info: #4AA8E8;
  --ava-info-dark: #A9D9FA;
  --ava-info-soft: #17344A;
  --ava-document: #8DB9C5;
  --surface-2: #152635;
  --surface-soft: #152635;
  --ink: var(--ava-text-primary);
  --text-muted: var(--ava-text-secondary);
  --white: var(--ava-surface);
  --ava-focus-color: #62B7EE;
  --shadow: 0 12px 28px rgba(0, 0, 0, .28);
}

html[data-theme="dark"] body,
html[data-theme="dark"] .app-shell,
html[data-theme="dark"] .workspace,
html[data-theme="dark"] .main-content { background: var(--ava-background); }

html[data-theme="dark"] :where(
  .card,.filter-bar,.metric-card,.kpi,.table-wrap,table,
  .selectable-item,.permission-group,.client-editor-actions,
  .client-billing-assignment,.client-assignment-row,.billing-product-list button,
  .billing-calculation-card,.billing-assignment-card>div,.field-inline-check,
  .receipt-evidence-item,.receipt-detail-summary>div,.delivery-history-row,
  .delivery-evidence-file,.claim-evidence-card,.consolidation-metric,
  .arrival-batch-card,.ava-action-dialog,.dialog,.empty-state,.empty,
  .server-volume,.server-policy-form
) {
  color: var(--ava-text-primary);
  background: var(--ava-surface);
  border-color: var(--ava-border);
}

html[data-theme="dark"] :where(
  .tab-bar,.module-tabs,.client-editor-tabs,.client-credit-toggle,
  .client-rate-head,.settings-tariff-head,.client-assignment-table-head,
  .billing-calculation-head,.billing-calculation-head>div,.ava-action-footer,
  .delivery-evidence-panel,.arrival-batch-card--all
) { color: var(--ava-text-secondary); background: var(--surface-2); border-color: var(--ava-border); }

html[data-theme="dark"] :where(
  .tab-button.is-active,.client-editor-tabs button.is-active,
  .settings-tariff-list button.is-active,.billing-product-list button.is-active,
  .selectable-item.is-active,.selectable-item:hover,.package-picker-row:hover,
  .package-picker-row.is-selected,.client-assignment-row.is-default
) { color: var(--ava-text-primary); background: var(--ava-primary-soft); border-color: var(--ava-interactive); }

html[data-theme="dark"] :where(
  .field>span,.checkbox-field,.client-editor-tabs button,
  .client-rate-head,.settings-tariff-head,.client-assignment-table-head,
  .arrival-selector-copy>span,.arrival-work-card span,.arrival-batch-copy>span,
  .package-trace-cell small,.incident-trace-card span,.consolidation-metric span,
  .billing-assignment-card>div span,.billing-calculation-head span
) { color: var(--ava-text-secondary); }

html[data-theme="dark"] table thead th { color: var(--ava-text-secondary); background: var(--surface-2); }
html[data-theme="dark"] table tbody tr:hover { background: #182B3C; }
html[data-theme="dark"] table tbody tr.is-selected,
html[data-theme="dark"] table tbody tr[aria-selected="true"] { background: var(--ava-primary-soft); }
html[data-theme="dark"] .server-volume-meter{background:#263b4e}
html[data-theme="dark"] .server-volume p,
html[data-theme="dark"] .server-volume>div:first-child span,
html[data-theme="dark"] .server-admin-workspace .card-header p,
html[data-theme="dark"] .server-technical-reset-card p{color:var(--ava-text-secondary)}
html[data-theme="dark"] .status--success{color:var(--ava-success-dark);background:var(--ava-success-soft)}
html[data-theme="dark"] .status--danger{color:var(--ava-danger-dark);background:var(--ava-danger-soft)}
html[data-theme="dark"] .inline-alert--success{color:var(--ava-success-dark);background:var(--ava-success-soft);border-color:#28785E}
html[data-theme="dark"] .inline-alert--warning{color:var(--ava-warning-dark);background:var(--ava-warning-soft);border-color:#8A5D20}

html[data-theme="dark"] input:not([type="checkbox"]):not([type="radio"]):not([type="file"]),
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
  color: var(--ava-text-primary);
  background: #0E1A26;
  border-color: var(--ava-border);
}
html[data-theme="dark"] input:disabled,
html[data-theme="dark"] select:disabled,
html[data-theme="dark"] textarea:disabled,
html[data-theme="dark"] input[readonly] { color: var(--ava-text-secondary); background: #172431; }

html[data-theme="dark"] :where(.callout,.package-trace-banner,.detail-item--trace,.incident-trace-card) {
  color: var(--ava-info-dark);
  background: var(--ava-info-soft) !important;
  border-color: #2B6688 !important;
}
html[data-theme="dark"] .callout--warning { color: var(--ava-warning-dark); background: var(--ava-warning-soft) !important; border-color: #8A5D20 !important; }
html[data-theme="dark"] .callout--danger { color: var(--ava-danger-dark); background: var(--ava-danger-soft) !important; border-color: #8F3C36 !important; }
html[data-theme="dark"] .callout--success { color: var(--ava-success-dark); background: var(--ava-success-soft) !important; border-color: #28785E !important; }

html[data-theme="dark"] :where(.button--danger,.button.danger):hover:not(:disabled) { border-color: #A9322B; background: #A9322B; }
html[data-theme="dark"] :where(.button--success,.view-expand-button,.statement-expand-button):hover:not(:disabled) { border-color: #17684E; background: #17684E; }
html[data-theme="dark"] .loading-layer { color: var(--ava-interactive); background: rgba(9, 19, 30, .86); }
html[data-theme="dark"] .dialog-layer { background: rgba(0, 8, 15, .72); }
html[data-theme="dark"] .package-trace-column { background: #221D2C !important; }

/* Cobertura de superficies heredadas. AVA conserva componentes de versiones
   anteriores que fijaban blancos y grises claros en vez de consumir tokens. */
html[data-theme="dark"] :where(
  .quick-incident-panel,.incident-create-mode,.incident-type-selector,
  .arrival-selector-card,.arrival-work-card,.statement-currency-group,
  .agency-package-filters,.ava-action-dialog,.ava-action-dialog--danger,.ava-action-footer,
  .billing-file-drop,.billing-selection-bar,.billing-receipt-credit-panel,
  .client-search-results,.client-rate-cell--readonly,.client-rate-note,
  .consolidation-metric,.dashboard-flow-step,.dashboard-action-list>button,
  .delivery-summary-card,.delivery-package-panel,.delivery-packages-table,
  .delivery-table-toolbar,.delivery-evidence-content,.delivery-form-heading>span,
  .dialog__body #warehouse-delivery-form,.dialog__body #delivery-evidence,
  .entry-mode-card,.finalize-work-summary div,.incident-case-shell,
  .incident-case-main,.incident-management-panel,.incident-created-record,
  .incident-evidence-card,.incident-history-card,.incident-metric-card,
  .incident-summary-strip,.list-pagination,.list-pagination button,
  .notification-item,.package-check-grid label,.package-source-owner,
  .receipt-detail-summary>div,.settings-tariff-transport-cell,
  .warranty-step,.work-banner,.client-billing-assignment,
  .client-assignment-table-head,.settings-tariff-head,
  .billing-assignment-card,.billing-calculation-card,.claim-evidence-card,
  .statement-workbench,.statement-filters,.statement-inline-summary,
  .warehouse-more-trigger,.warehouse-row-action-menu,.warehouse-quick-hint,
  .warehouse-quick-delivery,.warehouse-quick-action
) {
  color: var(--ava-text-primary);
  background: var(--ava-surface) !important;
  border-color: var(--ava-border) !important;
}

html[data-theme="dark"] :where(
  .login-view,.login-access-panel,.login-card,.login-input,
  .ava-about-card,.detail-panel,.connection-state,.dashboard-skeleton,
  .dashboard-status-item,.billing-credit-pool,.billing-credit-source,
  .billing-refresh-button,.billing-reconciliation,.calculated-field output,
  .audit-value-before,.audit-value-after,.incident-active-filter,
  .incident-process-note,.incident-evidence-preview,.invoice-preview-client,
  .logo-preview,.work-compact-strip,.evidence-link,.module-strip-heading .icon-button,
  .sidebar,.sidebar.is-open
) {
  color: var(--ava-text-primary);
  background: var(--ava-surface) !important;
  border-color: var(--ava-border) !important;
}

html[data-theme="dark"] :where(
  .quick-incident-panel,.incident-create-mode,.incident-type-selector,
  .arrival-selector-card,.arrival-work-card,.statement-currency-group,
  .agency-package-filters,.ava-action-footer,.billing-receipt-credit-panel,
  .client-rate-cell--readonly,.client-rate-note,.dashboard-flow-step,
  .delivery-table-toolbar,.dialog__body #warehouse-delivery-form,
  .dialog__body #delivery-evidence,.finalize-work-summary div,
  .incident-summary-strip,.package-check-grid label,.package-source-owner,
  .receipt-detail-summary>div,.settings-tariff-transport-cell,.work-banner,
  .client-assignment-table-head,.settings-tariff-head,
  .billing-assignment-card>div,.billing-calculation-head>div,
  .statement-filters,.warehouse-row-action-menu,.warehouse-quick-delivery,
  .warehouse-quick-action
) { background: var(--surface-2) !important; }

html[data-theme="dark"] .statement-inline-summary > span,
html[data-theme="dark"] .warehouse-quick-hint {
  color: var(--ava-info-dark) !important;
  background: var(--ava-info-soft) !important;
  border-color: #2B6688 !important;
}

html[data-theme="dark"] :where(
  .warehouse-more-trigger:hover,.warehouse-more-trigger:focus-visible,
  .warehouse-more-trigger[aria-expanded="true"],
  .warehouse-row-action-menu button:hover,.warehouse-row-action-menu button:focus-visible,
  .warehouse-quick-action:hover,.warehouse-quick-action:focus-visible,
  .warehouse-quick-action.is-recommended
) {
  color: var(--ava-text-primary) !important;
  background: #1B3042 !important;
  border-color: #4A657C !important;
}

html[data-theme="dark"] .warehouse-quick-delivery.is-voided {
  color: var(--ava-text-secondary) !important;
  background: #172431 !important;
  border-color: var(--ava-border) !important;
}

html[data-theme="dark"] .warehouse-row-action-menu button.is-danger,
html[data-theme="dark"] .warehouse-quick-action--danger {
  color: var(--ava-danger-dark) !important;
}
html[data-theme="dark"] :where(
  .warehouse-row-action-menu button.is-danger:hover,
  .warehouse-row-action-menu button.is-danger:focus-visible,
  .warehouse-quick-action--danger:hover,
  .warehouse-quick-action--danger:focus-visible,
  .warehouse-quick-action--danger.is-recommended
) {
  color: var(--ava-danger-dark) !important;
  background: var(--ava-danger-soft) !important;
  border-color: #8F3C36 !important;
}

html[data-theme="dark"] .billing-calculation-balance {
  color: var(--ava-success-dark) !important;
  background: var(--ava-success-soft) !important;
  border-color: #28785E !important;
}

html[data-theme="dark"] :where(
  .transport-badge,.warehouse-preview-trigger,.warehouse-quick-delivery-icon
) {
  color: var(--ava-info-dark) !important;
  background: var(--ava-primary-soft) !important;
  border-color: #2B6688 !important;
}
html[data-theme="dark"] :where(
  .warehouse-preview-trigger:hover,.warehouse-preview-trigger:focus-visible
) {
  color: var(--ava-text-primary) !important;
  background: #1B3042 !important;
  border-color: #4A657C !important;
}

html[data-theme="dark"] :where(
  .login-input,.connection-state,.dashboard-skeleton,.dashboard-status-item,
  .billing-credit-pool,.billing-credit-source,.billing-refresh-button,
  .incident-active-filter,.incident-process-note,.work-compact-strip,
  .module-strip-heading .icon-button
) { background: var(--surface-2) !important; }

html[data-theme="dark"] :where(
  .selectable-item,.billing-product-list button,.settings-tariff-list button,
  .client-search-option,.dashboard-action-list>button,.delivery-pagination button,
  .list-pagination button,.guide-chip,.compact-button,.button--secondary,
  .document-preview-toolbar button,.document-preview-toolbar select,
  .icon-button,.password-toggle,.module-tab
) {
  color: var(--ava-text-primary);
  background: var(--surface-2) !important;
  border-color: var(--ava-border) !important;
}

html[data-theme="dark"] :where(
  .selectable-item:hover,.billing-product-list button:hover,
  .settings-tariff-list button:hover,.client-search-option:hover,
  .client-search-option:focus,.client-search-option.is-highlighted,
  .dashboard-action-list>button:not(:disabled):hover,
  .delivery-evidence-content:hover,.delivery-pagination button:hover:not(:disabled),
  .list-pagination button:hover:not(:disabled),.guide-chip:hover,.compact-button:hover,
  .button--secondary:hover:not(:disabled),.icon-button:hover,.password-toggle:hover,
  .module-tab:hover,table tbody tr:hover
) {
  color: var(--ava-text-primary);
  background: #1B3042 !important;
  border-color: #4A657C !important;
}

html[data-theme="dark"] :where(
  .selectable-item.is-active,.billing-product-list button.is-active,
  .settings-tariff-list button.is-active,.client-editor-tabs button.is-active,
  .entry-mode-card.is-selected,.delivery-pagination button.is-active,
  .incident-metric-card.is-selected,.arrival-batch-card.is-active,
  .package-picker-row.is-selected,.package-picker-row:hover
) {
  color: var(--ava-text-primary) !important;
  background: var(--ava-primary-soft) !important;
  border-color: var(--ava-interactive) !important;
}

/* Texto operativo: corrige colores literales diseñados para fondos blancos. */
html[data-theme="dark"] :where(
  .quick-incident-title,.quick-incident-panel label,.incident-type-selector strong,
  .incident-type-selector label,.arrival-selector-copy>strong,
  .arrival-work-card strong,.arrival-batch-copy>strong,.arrival-batch-totals>strong,
  .consolidation-metric strong,.statement-metric .metric-copy strong,
  .delivery-summary-card strong,.delivery-evidence-content strong,
  .delivery-form-heading h3,.incident-summary-strip strong,
  .incident-metric-copy strong,.incident-step strong,.ava-action-body p,
  .billing-receipt-credit-summary strong,.receipt-detail-summary strong,
  .client-rate-cell--readonly strong,.entry-mode-card strong,
  .warranty-beneficiary strong,.work-banner-item strong,
  .warehouse-delivery-workspace,.claim-evidence-card,
  .billing-advanced-filters summary,.delivery-selection-count,
  .incident-activity-item p,.work-compact-copy strong
) { color: var(--ava-text-primary) !important; }

html[data-theme="dark"] :where(
  .quick-incident-help,.quick-incident-panel>div span,
  .arrival-selector-copy>span,.arrival-selector-copy>small,
  .arrival-work-card span,.arrival-work-card small,
  .arrival-batch-copy>span,.arrival-batch-copy>small,
  .arrival-batch-totals>small,.arrival-billing-progress>small,
  .consolidation-metric span,.consolidation-metric small,
  .statement-currency-group>header span,.statement-metric .metric-copy span,
  .delivery-summary-card>div>span,.delivery-evidence-content small,
  .delivery-evidence-empty,.delivery-form-heading p,.delivery-table-footer,
  .incident-summary-strip span,.incident-trace-card span,
  .incident-metric-copy span,.incident-step small,.ava-action-body>small,
  .package-source-owner span,.package-source-owner small,
  .receipt-detail-summary span,.warranty-beneficiary small,
  .work-banner-item span,.claim-evidence-card small,
  .ava-about-values span,.billing-receipt-credit-input>span,
  .client-currency-options>span,.client-search-empty,.dashboard-flow>i,
  .delivery-evidence-list small,.incident-activity-item small,
  .incident-date-range i,.incident-final-client small,
  .incident-stage-card>p,.weight-group-card>div:first-child span,
  .work-compact-copy small
) { color: var(--ava-text-secondary) !important; }

/* Mensajes, advertencias y estados conservan significado sin deslumbrar. */
html[data-theme="dark"] :where(.inline-alert,.alert-item,.notification-item.is-unread,
  .correction-banner,.detail-item--trace,.package-trace-banner,.incident-trace-card,
  .delivery-finance-note,.return-picker-message) {
  color: var(--ava-info-dark) !important;
  background: var(--ava-info-soft) !important;
  border-color: #2B6688 !important;
}
html[data-theme="dark"] :where(.dashboard-error,.callout--danger,.callout--error,
  .billing-calculation-card--error,.status-badge--danger,.status-pill--danger) {
  color: var(--ava-danger-dark) !important;
  background: var(--ava-danger-soft) !important;
  border-color: #8F3C36 !important;
}
html[data-theme="dark"] :where(.button--danger-outline) {
  color: var(--ava-danger-dark) !important;
  background: var(--ava-danger-soft) !important;
  border-color: #8F3C36 !important;
}
/* Los hijos tenían colores claros históricos que impedían heredar el estado. */
html[data-theme="dark"] .billing-calculation-card--error > strong,
html[data-theme="dark"] .billing-calculation-card--error > span {
  color: var(--ava-danger-dark);
}
html[data-theme="dark"] :where(.callout--warning,.status-badge--warning,
  .status-pill--warning,.delivery-status--held,.button--warning) {
  color: var(--ava-warning-dark) !important;
  background: var(--ava-warning-soft) !important;
  border-color: #8A5D20 !important;
}
html[data-theme="dark"] :where(.callout--success,.status-badge--success,
  .delivery-status--available,.scanner-feedback[data-tone="success"]) {
  color: var(--ava-success-dark) !important;
  background: var(--ava-success-soft) !important;
  border-color: #28785E !important;
}
html[data-theme="dark"] :where(.status-badge--neutral,.status-muted,
  .package-picker-row.is-unavailable,
  .status-badge:not(.status-badge--success):not(.status-badge--warning):not(.status-badge--danger):not(.status-ok),
  .delivery-status--delivered) {
  color: var(--ava-text-secondary) !important;
  background: #202D3A !important;
  border-color: var(--ava-border) !important;
}

html[data-theme="dark"] .audit-value-before {
  color: #D1DAE3 !important;
  background: #202D3A !important;
  border-color: #4A5E72 !important;
}
html[data-theme="dark"] .audit-value-after,
html[data-theme="dark"] :where(.billing-reconciliation .is-balanced) {
  color: var(--ava-success-dark) !important;
  background: var(--ava-success-soft) !important;
  border-color: #28785E !important;
}
html[data-theme="dark"] :where(.billing-reconciliation .is-different) {
  color: var(--ava-danger-dark) !important;
  background: var(--ava-danger-soft) !important;
  border-color: #8F3C36 !important;
}
html[data-theme="dark"] :where(.dashboard-trend--up,
  .work-banner-mode strong) { color: var(--ava-success-dark) !important; }

/* Iconos con tonos oscuros equivalentes; se evitan mosaicos blancos dentro de
   tarjetas oscuras sin perder la semántica azul, verde, ámbar y violeta. */
html[data-theme="dark"] :where(
  .arrival-selector-icon,.arrival-work-icon,.arrival-batch-icon,
  .digitization-kpi-icon,.consolidation-metric-icon,.metric-icon,
  .statement-icon,.statement-metric-icon,.delivery-summary-icon,
  .incident-metric-icon,.auto-card-icon,.entry-mode-icon,.kpi-mode-icon,
  .claim-evidence-file,.dashboard-activity-icon,.delivery-control-icon,
  .incident-trace-icon,.incident-client-search-icon,.incident-search-control .ui-icon
) { background: #17344A !important; }
html[data-theme="dark"] :where(
  .digitization-kpi--blue .digitization-kpi-icon,
  .consolidation-metric--blue .consolidation-metric-icon,
  .delivery-summary-card--blue .delivery-summary-icon,
  .arrival-batch-card--blue .arrival-batch-icon,.metric-icon--blue,
  .metric-icon--weight,.statement-icon--refresh
) { color: var(--ava-info-dark) !important; background: var(--ava-info-soft) !important; }
html[data-theme="dark"] :where(
  .digitization-kpi--green .digitization-kpi-icon,
  .consolidation-metric--green .consolidation-metric-icon,
  .delivery-summary-card--green .delivery-summary-icon,
  .arrival-batch-card--green .arrival-batch-icon,.metric-icon--green,
  .metric-icon--packages,.statement-icon--payment
) { color: var(--ava-success-dark) !important; background: var(--ava-success-soft) !important; }
html[data-theme="dark"] :where(
  .digitization-kpi--amber .digitization-kpi-icon,
  .consolidation-metric--amber .consolidation-metric-icon,
  .delivery-summary-card--amber .delivery-summary-icon,.metric-icon--amber,
  .metric-icon--pending,.statement-icon--alert
) { color: var(--ava-warning-dark) !important; background: var(--ava-warning-soft) !important; }
html[data-theme="dark"] :where(
  .digitization-kpi--violet .digitization-kpi-icon,
  .consolidation-metric--violet .consolidation-metric-icon,
  .delivery-summary-card--violet .delivery-summary-icon,
  .arrival-batch-card--violet .arrival-batch-icon,.metric-icon--violet,
  .metric-icon--invoices,.statement-icon--users,.package-trace-icon
) { color: #D4B5FF !important; background: #302442 !important; }
html[data-theme="dark"] :where(
  .consolidation-metric--coral .consolidation-metric-icon,
  .arrival-batch-card--coral .arrival-batch-icon,.metric-icon--red,
  .metric-icon--voided,.metric-icon--incidents
) { color: var(--ava-danger-dark) !important; background: var(--ava-danger-soft) !important; }

html[data-theme="dark"] input[type="checkbox"],
html[data-theme="dark"] input[type="radio"] { accent-color: var(--ava-interactive); }
html[data-theme="dark"] button:disabled { opacity: .68; }
html[data-theme="dark"] ::placeholder { color: #8FA1B3; opacity: 1; }

/* El visor pertenece a la aplicación y usa gris oscuro; únicamente las hojas
   renderizadas se mantienen blancas porque representan el documento impreso. */
html[data-theme="dark"] :where(.document-preview-shell,.document-preview-toolbar,
  .document-preview-dialog-body .invoice-document-preview,
  .document-preview-dialog-body .document-preview-pages) {
  color: var(--ava-text-primary);
  background: #0E1A26 !important;
  border-color: var(--ava-border) !important;
}
html[data-theme="dark"] .document-preview-toolbar output { color: var(--ava-text-primary); }
html[data-theme="dark"] .document-preview-page,
html[data-theme="dark"] .document-preview-image,
html[data-theme="dark"] .invoice-paper-preview {
  color: #182230;
  background: #FFFFFF !important;
  color-scheme: light;
}

.theme-toggle-button { position: relative; width: 40px; min-width: 40px; display: grid; place-items: center; padding: 0; }
.theme-toggle-button svg { position: absolute; width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; transition: opacity 140ms ease, transform 140ms ease; }
html[data-theme="light"] .theme-icon--sun,
html[data-theme="dark"] .theme-icon--moon { opacity: 0; transform: scale(.7) rotate(-18deg); }
html[data-theme="light"] .theme-icon--moon,
html[data-theme="dark"] .theme-icon--sun { opacity: 1; transform: scale(1) rotate(0); }
