/* Appearance controls in the account menu. Existing light styles stay authoritative. */
.app-theme-picker {
    display: grid;
    gap: .35rem;
    margin: 0;
    padding: .7rem 1rem;
    border-top: 1px solid #d8e4f0;
    color: #526477;
    font-size: .8rem;
    font-weight: 600;
}
.app-theme-options { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .35rem; }
.app-theme-options button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-width: 0;
    min-height: 44px;
    padding: .45rem .55rem;
    border: 1px solid #c8d9ea;
    border-radius: 8px;
    background: #f8fbfe;
    color: #334155;
    font: inherit;
    cursor: pointer;
}
.app-theme-options button:hover { background: #eaf5ff; border-color: #93b8dc; }
.app-theme-options button[aria-pressed="true"] { background: #dceeff; color: #075aa9; border-color: #76a8da; }
.app-theme-options button:focus-visible { outline: 3px solid #70a8e0; outline-offset: 2px; }
.account-menu { max-height: calc(100dvh - 100px); overflow-y: auto; overscroll-behavior: contain; }

/* Screen-only tokens keep browser printing and generated manuals in their light palette. */
@media screen {
    :root[data-theme="light"] { color-scheme: light; }
    :root[data-theme="dark"] {
        color-scheme: dark;
        --theme-bg: #0c1422;
        --theme-surface: #152235;
        --theme-raised: #1c2c42;
        --theme-hover: #253b56;
        --theme-input: #101c2c;
        --theme-text: #e5edf7;
        --theme-muted: #a5b5c9;
        --theme-border: #3c516a;
        --theme-card-edge: #4b6380;
        --theme-link: #8ac5ff;
        --theme-link-bg: #183c5d;
        --theme-success: #87e5b5;
        --theme-success-bg: #153d30;
        --theme-danger: #ffaaaa;
        --theme-danger-bg: #49272e;
        --theme-warning: #f7d58b;
        --theme-warning-bg: #443720;
        --admin-bg-a: var(--theme-bg);
        --admin-bg-b: #101e30;
        --admin-surface: var(--theme-surface);
        --admin-text: var(--theme-text);
        --admin-muted: var(--theme-muted);
        --admin-border: var(--theme-border);
        --admin-success-bg: var(--theme-success-bg);
        --admin-success-text: var(--theme-success);
        --admin-error-bg: var(--theme-danger-bg);
        --admin-error-text: var(--theme-danger);
        --admin-warning-bg: var(--theme-warning-bg);
        --admin-warning-text: var(--theme-warning);
        background: var(--theme-bg);
        color: var(--theme-text);
        scrollbar-color: #637a95 var(--theme-bg);
    }
    :root[data-theme="dark"] body {
        color: var(--theme-text);
        background: radial-gradient(ellipse at top left, #152d48, transparent 65%), var(--theme-bg);
    }
    :root[data-theme="dark"] :is(h1, h2, h3, h4, legend, .admin-field label, .input-group label,
        .admin-field-inline label, .admin-form-section > legend, .admin-checkbox-filter,
        .account-menu-name, .product-title, .product-code-value, .product-price-current, .cart-title,
        .cart-item-title, .cart-item-total-price, .cart-summary-value,
        .cart-review-item-details h3, .cart-review-grand-total dt, .cart-review-grand-total dd,
        .inquiry-product-code, .recommendations-partner-name, .admin-suggestion-title,
        .text-gray-700, .text-gray-800, .text-gray-900) { color: var(--theme-text); }

    /* Main surfaces, including mobile table cards and the native inquiry dialog. */
    :root[data-theme="dark"] :is(.app-shell-inner, .app-shell-filters, .account-menu,
        .app-shell-category-choices, .catalogue-advanced-filters, .shopping-cart,
        .cart-item, .product-card, .product-content, .catalog-loader-content, .admin-panel-card,
        .admin-table, .admin-row-menu-panel, .admin-suggestions, .admin-suggestion-button,
        .exclusions-search-results, .exclusions-search-result, .exclusions-item,
        .recommendations-source, .recommendations-active, .recommendations-item,
        .recommendations-source-item, .customer-recommendations-stats > div,
        .inquiry-detail-modal, .inquiry-detail-actions, .cart-review-card,
        .cart-review-summary-card, .cart-review-empty, .help-content, .help-sidebar,
        .login-box, .bg-white) {
        background: var(--theme-surface);
        border-color: var(--theme-border);
        color: var(--theme-text);
    }
    :root[data-theme="dark"] :is(.customers-page tbody tr, .inquiries-page tbody tr,
        .inquiry-detail-items-table tbody tr, .admin-mobile-table tbody tr) {
        background: var(--theme-surface);
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] :is(.app-shell-header.is-catalogue .app-shell-toolbar-slot,
        .recommendations-save-bar) {
        background: var(--theme-surface);
        border-color: var(--theme-border);
        box-shadow: 0 12px 28px #0005;
    }
    :root[data-theme="dark"] :is(.admin-panel-header, .catalogue-advanced-heading,
        .admin-table thead th, .inquiry-detail-header, .inquiry-detail-meta div,
        .cart-header, .cart-footer, .cart-summary, .admin-options-box,
        .exclusions-editor, .exclusions-current, .recommendations-partner-header,
        .customer-recommendations-card, .help-note, .help-table th,
        .help-section figure, .catalogue-advanced-field, .bg-gray-50, .bg-gray-100) {
        background: var(--theme-raised);
        color: var(--theme-text);
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] :is(.admin-footer, .catalog-loader-overlay) { background: #0c1422f2; }
    :root[data-theme="dark"] :is(.admin-form-section, .admin-table-wrap, .admin-table tbody td,
        .account-menu-header, .app-theme-picker, .catalogue-advanced-actions,
        .inquiry-detail-note-wrap, .inquiry-detail-header, .inquiry-detail-actions,
        .help-section + .help-section, .help-table th, .help-table td,
        .cart-review-section-header, .cart-review-item, .cart-review-summary-list > div,
        .cart-review-summary-list .cart-review-net-total, .cart-summary-total,
        .cart-summary-net, .admin-suggestion-button, .exclusions-search-result) {
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] .inquiry-detail-items-table tfoot :is(tr, td) {
        background: var(--theme-raised);
        border-color: var(--theme-border);
        color: var(--theme-text);
    }

    /* Native inputs/selects inherit the dark color-scheme; photos are never recolored. */
    :root[data-theme="dark"] :is(.admin-field input, .admin-field select, .admin-field textarea,
        .admin-field-inline input, .admin-field-inline select, .input-field,
        .catalogue-advanced-field input[type="number"], .app-theme-options button,
        .help-sidebar input, .help-mobile-options select, .cart-review-note-card textarea,
        .exclusions-list-header input, .admin-email-tags) {
        background: var(--theme-input);
        color: var(--theme-text);
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] .header-control {
        background: var(--theme-input) !important;
        color: var(--theme-text) !important;
        border-color: var(--theme-border) !important;
    }
    :root[data-theme="dark"] ::placeholder { color: var(--theme-muted); opacity: 1; }
    :root[data-theme="dark"] :is(input, select, textarea) { accent-color: #60a5fa; }
    :root[data-theme="dark"] :is(input, select, textarea):focus-visible {
        outline: 2px solid var(--theme-link);
        outline-offset: 2px;
    }
    :root[data-theme="dark"] .admin-email-tags input[data-email-tags-entry] { background: transparent; }
    :root[data-theme="dark"] :is(input.input-field, .admin-field input):-webkit-autofill {
        -webkit-text-fill-color: var(--theme-text);
        box-shadow: 0 0 0 100px var(--theme-input) inset;
        caret-color: var(--theme-text);
    }
    :root[data-theme="dark"] :is(.product-card-quantity-controls, .cart-quantity-controls,
        .modal-product-quantity-controls, .cart-review-quantity) {
        background: var(--theme-input);
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] :is(.product-card .product-card-quantity-button, .cart-quantity-btn,
        .modal-quantity-btn, .cart-review-quantity button) {
        background: var(--theme-raised);
        color: var(--theme-text);
    }
    :root[data-theme="dark"] :is(.product-card-quantity-input, .cart-quantity-input,
        .modal-quantity-input, .cart-review-quantity input) {
        background: var(--theme-input);
        color: var(--theme-text);
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] :is(.product-card .product-card-quantity-button:hover,
        .cart-quantity-btn:hover, .modal-quantity-btn:hover, .cart-review-quantity button:hover,
        .product-card-quantity-input:focus, .cart-quantity-input:focus, .modal-quantity-input:focus) {
        background: var(--theme-hover);
        color: var(--theme-text);
    }

    /* Secondary actions, navigation and their selected/hover states. */
    :root[data-theme="dark"] :is(.admin-btn-secondary, .admin-btn-ghost,
        .admin-nav-link, .header-cart-button, .inquiry-detail-close, .cart-close-btn,
        .app-shell-mobile-filter-toggle, .app-shell-category-picker > summary,
        .catalogue-advanced-filters button, .customers-filter-options > summary,
        .recommendations-icon-button, .help-guides a, .help-contents summary,
        .catalogue-load-more-button, .app-shell-stock-filter) {
        background: var(--theme-raised);
        color: var(--theme-text);
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] .app-shell-admin-nav .admin-nav-link {
        background: var(--theme-raised);
        color: var(--theme-text);
        border-color: var(--theme-border);
    }
    :root[data-theme="dark"] :is(.admin-btn-secondary:hover, .admin-btn-ghost:hover,
        .admin-nav-link:hover, .header-cart-button:hover, .inquiry-detail-close:hover,
        .cart-close-btn:hover, .recommendations-icon-button:hover:not(:disabled),
        .catalogue-load-more-button:hover:not(:disabled), .app-shell-stock-filter:hover,
        .admin-suggestion-button:hover, .admin-suggestion-button:focus,
        .exclusions-search-result:hover:not(:disabled), .app-shell-category-choices label:hover,
        .admin-table tbody tr:hover, .account-menu-item-neutral:hover, .help-guides a:hover,
        .help-contents a:hover, .app-theme-options button:hover) {
        background: var(--theme-hover);
        color: var(--theme-text);
    }
    :root[data-theme="dark"] :is(.admin-nav-link.is-active, .account-menu-item.is-current,
        .app-shell-advanced-toggle.is-active, .app-shell-mobile-filter-toggle[aria-expanded="true"],
        .help-guides a[aria-current], .help-contents a[aria-current="location"],
        .admin-email-tag, .exclusions-all-hint, .app-theme-options button[aria-pressed="true"]) {
        background: var(--theme-link-bg);
        color: var(--theme-link);
        border-color: #5281aa;
    }
    :root[data-theme="dark"] .app-shell-admin-nav .admin-nav-link.is-active {
        background: var(--theme-link-bg);
        color: var(--theme-link);
        border-color: #5281aa;
    }
    :root[data-theme="dark"] :is(.catalogue-advanced-filters .catalogue-advanced-apply,
        .catalogue-advanced-filters .catalogue-advanced-apply:hover) {
        background: #0f6bcb;
        color: #fff;
        border-color: #368bdf;
    }
    :root[data-theme="dark"] :is(.header-cart-button svg, .account-menu-item-neutral,
        .help-eyebrow, .help-section h2, .help-section h3, .help-heading h1,
        .help-contents a, .catalogue-advanced-heading h2, .cart-review-eyebrow,
        .exclusions-search-result em, .admin-email-tag-remove, .admin-table-sort:hover,
        .cart-item-title:hover) { color: var(--theme-link); }
    :root[data-theme="dark"] :is(.account-button, .admin-btn-primary, .cart-review-submit,
        .cart-send-btn, .swal2-popup .swal2-styled.swal2-confirm) {
        background: linear-gradient(135deg, #0f6bcb, #126ca5);
        color: #fff;
    }
    :root[data-theme="dark"] .product-button { background-color: #08774f !important; }
    :root[data-theme="dark"] :is(.modal-product-add-button, .modal-product-add-button:hover) {
        background: #08774f;
        color: #fff;
    }
    :root[data-theme="dark"] a:not([class]) { color: var(--theme-link); }
    :root[data-theme="dark"] .sort-direction-btn:hover { background: var(--theme-hover) !important; }
    :root[data-theme="dark"] .catalogue-advanced-field .catalogue-availability-control {
        background: var(--theme-input);
        color: var(--theme-text);
    }
    :root[data-theme="dark"] .exclusions-divider::before,
    :root[data-theme="dark"] .exclusions-divider::after { background: var(--theme-border); }
    :root[data-theme="dark"] .admin-table-sort[data-sort-active="true"] .admin-sort-indicator::before {
        color: var(--theme-link);
    }

    /* Supporting text remains legible on the darker surfaces. */
    :root[data-theme="dark"] :is(.product-meta, .product-meta-label, .product-price-original,
        .catalog-loader-text, .cart-empty-text, .cart-summary-label, .cart-item-price-per-unit,
        .catalogue-products-counter, .catalogue-price-range label, .catalogue-advanced-field legend,
        .app-shell-category-choices label, .admin-field-inline label, .admin-table-meta,
        .admin-loading, .admin-pagination-info, .inquiries-note, .inquiry-product-name,
        .inquiry-detail-note-wrap p, .recommendations-source-item, .recommendations-position,
        .recommendations-drag, .recommendations-description small, .recommendations-partner-header p,
        .recommendations-section-head p, .exclusions-search-result small, .exclusions-item span,
        .exclusions-search-empty, .exclusions-list-header p, .customer-recommendations-partner span,
        .customer-recommendations-stats span, .customer-recommendations-stats small,
        .customer-recommendations-empty, .help-page, .help-version, .help-search-status,
        .help-sidebar-note, .help-reading-note, .help-section figcaption,
        .app-theme-picker, .login-password-toggle, .admin-footer-inner, .text-gray-400, .text-gray-500, .text-gray-600) {
        color: var(--theme-muted);
    }
    :root[data-theme="dark"] :is(.customers-page, .inquiries-page,
        .inquiry-detail-items-table, .admin-mobile-table) td::before { color: var(--theme-muted); }
    :root[data-theme="dark"] :is(.cart-summary-net .cart-summary-label,
        .cart-summary-net .cart-summary-value, .cart-review-net-total dt, .cart-review-net-total dd) {
        color: var(--theme-text);
    }

    /* Keep status meaning consistent; the foreground/background pairs are theme tokens. */
    :root[data-theme="dark"] :is(.admin-btn-warning, .admin-badge-warning, .admin-badge-country-bih,
        .cart-review-stock-warning, .admin-field-warning) {
        background: var(--theme-warning-bg);
        color: var(--theme-warning);
        border-color: #a17c35;
    }
    :root[data-theme="dark"] :is(.admin-btn-warning:hover, .cart-review-quantity input.is-over-stock,
        .cart-quantity-input.is-over-stock, .product-card-quantity-input.is-over-stock,
        .modal-product-quantity-controls .modal-quantity-input.is-over-stock) {
        background: var(--theme-warning-bg);
        color: var(--theme-warning);
        border-color: #bb9139;
        box-shadow: inset 0 0 0 1px #bb9139;
    }
    :root[data-theme="dark"] :is(.admin-badge-success, .admin-badge-country-slo,
        .product-discount-badge, .cart-review-unit-price span, .bg-green-50) {
        background: var(--theme-success-bg);
        color: var(--theme-success);
    }
    :root[data-theme="dark"] :is(.admin-badge-danger, .admin-badge-country-srb,
        .cart-clear-btn, .cart-remove-button, .exclusions-remove, .bg-red-50,
        .product-card .product-card-quantity-decrease.is-remove,
        .cart-quantity-decrease.is-remove, .modal-product-quantity-controls .modal-quantity-decrease.is-remove,
        .cart-review-quantity button.is-remove) {
        background: var(--theme-danger-bg);
        color: var(--theme-danger);
        border-color: #9d5963;
    }
    :root[data-theme="dark"] :is(.admin-badge-muted, .admin-badge-country-eng) {
        background: var(--theme-hover);
        color: var(--theme-text);
    }
    :root[data-theme="dark"] .admin-badge-country-hr {
        background: var(--theme-link-bg);
        color: var(--theme-link);
    }
    :root[data-theme="dark"] .exclusions-country-badge {
        background: var(--theme-link-bg);
        color: var(--theme-link) !important;
    }
    :root[data-theme="dark"] .exclusions-country-badge.is-global {
        background: #352955;
        color: #d5b7ff !important;
    }
    :root[data-theme="dark"] :is(.product-stock-row > span:last-child, .cart-item-price,
        .cart-total-price, .cart-review-unit-price strong, .text-green-600, .text-green-700,
        #inquiryDetailStatus[data-status="sent"]) { color: var(--theme-success); }
    :root[data-theme="dark"] :is(.account-menu-item-danger, .cart-review-clear, .cart-review-remove,
        .recommendations-icon-button.is-remove, .product-stock-row > .product-stock-value.is-empty,
        .text-red-500, .text-red-600, .text-red-700, #inquiryDetailStatus[data-status="failed"]) {
        color: var(--theme-danger);
    }
    :root[data-theme="dark"] .account-menu-item-danger:hover { background: var(--theme-danger-bg); }
    :root[data-theme="dark"] :is(.cart-stock-warning, .product-card-stock-warning,
        .recommendation-run-confirm) { color: var(--theme-warning); }

    /* ERP generator has a few page-local styles that load after this sheet. */
    :root[data-theme="dark"] :is(.recommendation-run-summary, .recommendation-country-preview) {
        background: var(--theme-raised);
        border-color: var(--theme-border);
        color: var(--theme-text);
    }
    :root[data-theme="dark"] :is(.recommendation-run-mode small, .recommendation-country-previews > p,
        .recommendation-country-preview summary span, .recommendation-country-table td:nth-child(2) span) {
        color: var(--theme-muted);
    }
    :root[data-theme="dark"] :is(.recommendation-run-mode, .recommendation-country-table-wrap,
        .recommendation-country-table th, .recommendation-country-table td) { border-color: var(--theme-border); }
    :root[data-theme="dark"] .recommendation-run-result.is-success {
        background: var(--theme-success-bg);
        color: var(--theme-success);
    }
    :root[data-theme="dark"] .recommendation-run-result.is-error {
        background: var(--theme-danger-bg);
        color: var(--theme-danger);
    }

    /* SweetAlert uses its own tokens, including alerts opened inside <dialog>. */
    :root[data-theme="dark"] .swal2-popup {
        --swal2-background: var(--theme-surface);
        --swal2-color: var(--theme-text);
        --swal2-border: 1px solid var(--theme-border);
        --swal2-input-background: var(--theme-input);
        --swal2-input-border: 1px solid var(--theme-border);
        --swal2-validation-message-background: var(--theme-danger-bg);
        --swal2-validation-message-color: var(--theme-danger);
        --swal2-footer-border-color: var(--theme-border);
        --swal2-timer-progress-bar-background: #a5b5c980;
        box-shadow: 0 12px 36px #0006;
    }
    :root[data-theme="dark"] .swal2-popup .swal2-title { color: var(--theme-text); }
    :root[data-theme="dark"] :is(.swal2-input, .swal2-textarea, .swal2-select) { color: var(--theme-text); }
    :root[data-theme="dark"] .swal2-popup .swal2-styled.swal2-cancel {
        background: var(--theme-hover);
        color: var(--theme-text);
    }

    /* A subtle edge separates cards from the page without changing their dimensions. */
    :root[data-theme="dark"] :is(.admin-panel-card, .cart-review-card, .cart-review-summary-card,
        .cart-review-empty, .help-content, .help-sidebar, .login-box, .catalogue-advanced-filters,
        .customer-recommendations-card, .recommendations-source, .recommendations-active,
        .recommendation-country-preview, .exclusions-editor, .exclusions-current, .cart-item) {
        border-color: var(--theme-card-edge);
        box-shadow: 0 6px 18px #0004;
    }
    :root[data-theme="dark"] .product-card {
        /* Draw above the content, which can cover an inset shadow at the bottom. */
        outline: 1px solid var(--theme-card-edge);
        outline-offset: -1px;
        box-shadow: 0 6px 18px #0004;
    }
    :root[data-theme="dark"] :is(.customers-page, .inquiries-page,
        .inquiry-detail-items-table, .admin-mobile-table) tbody tr { border-color: var(--theme-card-edge); }

    /* A white backing preserves transparent product photos; no invert/brightness filters. */
    :root[data-theme="dark"] :is(.product-image-container, .cart-item-image,
        .cart-review-item-media, .inquiry-product-image, .gallery-item) { background: #fff; }
    :root[data-theme="dark"] .cart-review-item-media { border-color: var(--theme-border); }
    :root[data-theme="dark"] :is(.product-image-container, .cart-review-item-media, .cart-item-image) .no-image-placeholder {
        color: #526477;
    }
    :root[data-theme="dark"] .catalog-spinner { border-color: var(--theme-border); border-top-color: var(--theme-link); }
    :root[data-theme="dark"] .recommendations-item.is-just-moved { animation-name: theme-recommendations-moved; }
    @keyframes theme-recommendations-moved {
        0%, 35% { background: var(--theme-link-bg); border-color: var(--theme-link); box-shadow: 0 0 0 3px #5281aa55; }
        100% { background: var(--theme-surface); border-color: var(--theme-border); box-shadow: none; }
    }

    @media (max-width: 760px) {
        :root[data-theme="dark"] #resetFilters.app-shell-reset {
            background: var(--theme-raised) !important;
            color: var(--theme-text) !important;
            border-color: var(--theme-border) !important;
        }
    }
}
@media print { .app-theme-picker { display: none; } }
