:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #11151d;
    --card: #1b212c;
    --surface-1: #252c38;
    --surface-hover: #303846;
    --surface-subtle: #202733;
    --text: #e5eaf1;
    --text-secondary: #c7d0dc;
    --muted: #a0aaba;
    --border: #3a4554;
    --border-strong: rgba(226, 232, 240, .2);
    --primary: #4f46e5;
    --primary-dark: #6366f1;
    --primary-ink: #b8c0ff;
    --primary-soft: #302d52;
    --surface-2: #303846;
    --surface-muted: #202733;
    --shelf: #2b3340;
    --danger: #b91c1c;
    --danger-ink: #fca5a5;
    --danger-strong: #f87171;
    --danger-button: #991b1b;
    --danger-bg: #45282c;
    --badge-danger-bg: #45282c;
    --danger-border: #713b43;
    --success: #047857;
    --success-ink: #6ee7b7;
    --success-button: #166534;
    --success-bg: #203b32;
    --badge-success-bg: #203b32;
    --success-border: #315849;
    --warning: #92400e;
    --warning-ink: #fcd34d;
    --warning-button: #92400e;
    --warning-bg: #40351f;
    --badge-warning-bg: #40351f;
    --warning-border: #65512b;
    --info: #1d4ed8;
    --info-ink: #93c5fd;
    --info-button: #1d4ed8;
    --primary-border: #4c4a73;
    --info-border: #365371;
    --info-bg: #20354b;
    --purple-bg: #352c4a;
    --violet-bg: #352c4a;
    --violet-border: #514067;
    --violet-ink: #c4b5fd;
    --teal-bg: #183b38;
    --teal-border: #315b55;
    --teal-ink: #5eead4;
    --badge-archive-bg: #65512b;
    --badge-archive-ink: #fde68a;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, .28);
    --shadow-pop: 0 12px 28px rgba(0, 0, 0, .45);
}

html[data-theme="dark"] body {
    background-color: var(--bg);
    color: var(--text);
}

html[data-theme="dark"] :is(input, select, textarea) {
    color-scheme: dark;
    color: var(--text);
    background-color: var(--card);
    border-color: var(--border);
}

html[data-theme="dark"] :is(input, select, textarea)::placeholder {
    color: var(--muted);
}

html[data-theme="dark"] :is(.app-header, .shell-header, .bnav, .side-nav, .bmenu__sheet) {
    color: var(--text);
    background-color: var(--card);
    border-color: var(--border);
}

html[data-theme="dark"] .bnav__item:hover,
html[data-theme="dark"] .side-nav__mini-link:hover {
    background-color: var(--surface-hover);
    color: var(--text);
}

html[data-theme="dark"] #tab-chat .chat-date-separator {
    background: var(--surface-2);
    border-color: var(--border);
    color: var(--text-secondary);
}

.bmenu__item.bmenu__theme {
    width: 100%;
    border: 0;
    font: inherit;
    text-align: left;
    cursor: pointer;
}

/* Тёмная шапка детали использует те же фоны стадий, что и заголовки колонок доски. */
html[data-theme="dark"] #tab-requests .zay-detail-head { box-shadow: 0 5px 18px rgba(0, 0, 0, .28); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--new   { background: var(--primary-soft); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--wait   { background: var(--warning-bg); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--work   { background: var(--info-bg); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--rev    { background: var(--badge-archive-bg); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--done   { background: var(--success-bg); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--rej    { background: var(--danger-bg); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--canc   { background: var(--surface-2); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--violet { background: var(--violet-bg); }
html[data-theme="dark"] #tab-requests .zay-detail-head.zay-head--teal   { background: var(--teal-bg); }

/* Сохраняем исходную светлую палитру компонентов, у которых цвет закреплён контрактными CSS-проверками. */
html[data-theme="dark"] #tab-requests .zay-badge--rej,
html[data-theme="dark"] #tab-requests .zay-gone,
html[data-theme="dark"] #zayTplRoot .zay-gone {
    background: var(--danger-bg);
    color: var(--danger-ink);
}

html[data-theme="dark"] #zayTplRoot .zay-tpl-note,
html[data-theme="dark"] #zayTplRoot .zay-tpl-msg--warn,
html[data-theme="dark"] #zayTplRoot .zay-tpl-warnbar,
html[data-theme="dark"] #zayTplRoot .zay-slot-lock,
html[data-theme="dark"] #zayTplRoot .zay-slot-warn {
    background: var(--warning-bg);
    color: var(--warning-ink);
}


/* Dark-mode coverage for controls and cards added on the current dev branch. */
html[data-theme="dark"] #tab-messages .chat-search,
html[data-theme="dark"] #tab-messages .chat-messenger-action,
html[data-theme="dark"] #tab-messages .chat-messenger-target {
    background-color: var(--card);
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-messages .chat-filter-control select { color-scheme: dark; }
html[data-theme="dark"] #tab-messages .chat-row__channel {
    background: var(--primary-soft);
    color: var(--primary-ink);
}
html[data-theme="dark"] #tab-messages .chat-messenger-action:hover,
html[data-theme="dark"] #tab-messages .chat-messenger-target:hover {
    background: var(--surface-hover);
    border-color: var(--primary-border);
}
html[data-theme="dark"] #tab-messages .chat-messenger-linkbox { border-color: var(--border); }
html[data-theme="dark"] #tab-messages .chat-messenger-target { border-bottom-color: var(--border); }
html[data-theme="dark"] #tab-messages .chat-messenger-target small { color: var(--muted); }

html[data-theme="dark"] #tab-entities { background: var(--bg); color: var(--text); }
html[data-theme="dark"] #tab-entities :is(.ent-types, .ent-main, .ent-dialog, .ent-links-popover, .ent-search-clear, .ent-modal__close, .ent-btn:not(.ent-btn--primary)),
html[data-theme="dark"] .ent-picker {
    background-color: var(--card);
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-entities :is(.ent-type:hover, .ent-type.is-active, .ent-table tr[data-record]:hover, .ent-table tbody tr:hover td, .ent-links-popover .ent-record-link:hover),
html[data-theme="dark"] .ent-picker__table tbody tr:hover td {
    background: var(--primary-soft);
    color: var(--primary-ink);
}
html[data-theme="dark"] #tab-entities :is(.ent-btn--primary, .crm-add) {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}
html[data-theme="dark"] #tab-entities :is(.ent-record-link, .ent-icon-btn:hover:not(:disabled), .ent-search-clear:hover, .ent-btn:hover),
html[data-theme="dark"] .ent-picker :is(.ent-record-link, .ent-icon-btn:hover:not(:disabled)) {
    color: var(--primary-ink);
}
html[data-theme="dark"] #tab-entities :is(.ent-record-links__more, .ent-type:hover, .ent-type.is-active, .ent-icon-btn:hover:not(:disabled), .ent-search-clear:hover) {
    border-color: var(--primary-border);
}
html[data-theme="dark"] #tab-entities :is(.ent-table th, .ent-table td, .ent-fields__row, .ent-toolbar, .ent-dialog__head, .ent-picker__head) {
    border-color: var(--border);
}
html[data-theme="dark"] #tab-entities :is(.ent-table th, .ent-picker__table th) { background: var(--surface-2); color: var(--muted); }
html[data-theme="dark"] #tab-entities .ent-notice { background: var(--warning-bg); color: var(--warning-ink); }
html[data-theme="dark"] #tab-entities .ent-btn--danger,
html[data-theme="dark"] .ent-picker__message.is-error,
html[data-theme="dark"] .ent-picker__status.is-error { color: var(--danger-ink); }
html[data-theme="dark"] .ent-picker__status:not(.is-error) { color: var(--success-ink); }

html[data-theme="dark"] #tab-tasks :is(.eh-tab, .eh-row, .por-task-drawer, .por-massbar, .eh-search, .por-zone, .task-card-layout-editor, .task-card-layout-editor__row, .notify-banner__btn, .task__posted, .por-edit-btn) {
    background-color: var(--card);
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-tasks .task-card-layout-editor__row select {
    background-color: var(--card);
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-tasks .por-modal {
    background: var(--card);
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-tasks .por-modal__head {
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-tasks :is(.eh-row:hover, .eh-row.is-selected, .eh-row.is-selected:hover, .por-massbar__clear:hover, .eh-search-x:hover, .task-card-layout-editor__row:hover) {
    background-color: var(--surface-hover);
    border-color: var(--border);
}
html[data-theme="dark"] #tab-tasks :is(.eh-pill, .eh-row.is-selected) { background: var(--primary-soft); color: var(--primary-ink); }
html[data-theme="dark"] #tab-tasks .eh-pill--review,
html[data-theme="dark"] #tab-tasks .task__due-pill.due--warning { background: var(--warning-bg); color: var(--warning-ink); }
html[data-theme="dark"] #tab-tasks .eh-pill--done { background: var(--success-bg); color: var(--success-ink); }
html[data-theme="dark"] #tab-tasks .eh-pill--muted { background: var(--surface-2); color: var(--muted); }
html[data-theme="dark"] #tab-tasks :is(.due--danger, .task__due-pill.due--danger, .task__req, .task__result-err, .task-card-layout-editor__error) { color: var(--danger-ink); }
html[data-theme="dark"] #tab-tasks :is(.task__due-pill.due--danger, .por-btn-danger:hover) { background: var(--danger-bg); }
html[data-theme="dark"] #tab-tasks .due--warning { color: var(--warning-ink); }
html[data-theme="dark"] #tab-tasks .eh-row--warning::before { background-color: var(--warning-ink); }
html[data-theme="dark"] #tab-tasks :is(.task__sec .ic, .por-massbar__clear:hover, .eh-check:not(.is-on)) { background-color: var(--primary-soft); color: var(--primary-ink); }
html[data-theme="dark"] #tab-tasks :is(.task__result-link, .por-massbar__clear, .task__attach-btn) { color: var(--primary-ink); }
html[data-theme="dark"] #tab-tasks .task__attach-btn:hover { background: var(--purple-bg); }
html[data-theme="dark"] #tab-tasks .task__done-btn.is-marked { background: var(--surface-2); color: var(--text); }
html[data-theme="dark"] #tab-tasks .eh-search-x { background: var(--surface-2); color: var(--muted); }
html[data-theme="dark"] #tab-tasks .notify-banner__btn { color: var(--primary-ink); }
html[data-theme="dark"] #tab-tasks .btn--secondary { background-color: var(--surface-2); color: var(--text); }

/* Fix light-only surfaces reported on the dark-theme screenshots. */
html[data-theme="dark"] #tab-entities .ent-table-scroll {
    background: var(--card);
}
html[data-theme="dark"] #tab-entities .ent-table td {
    background: var(--card);
}
html[data-theme="dark"] #tab-entities .ent-table tbody tr:hover td {
    background: var(--surface-hover);
}
html[data-theme="dark"] #tab-entities .ent-table th {
    background: var(--surface-2);
    color: var(--muted);
}

html[data-theme="dark"] #tab-messages .chat-filter-control select,
html[data-theme="dark"] #tab-messages .chat-dlg,
html[data-theme="dark"] #tab-messages .chat-dlg__icon,
html[data-theme="dark"] #tab-messages .chat-messenger-action,
html[data-theme="dark"] #tab-messages .chat-messenger-target {
    background-color: var(--card);
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-messages .chat-row__ttl,
html[data-theme="dark"] #tab-messages .chat-dlg__ttl,
html[data-theme="dark"] #tab-messages .chat-dlg__title-link {
    color: var(--text);
}
html[data-theme="dark"] #tab-messages .chat-row__pv,
html[data-theme="dark"] #tab-messages .chat-row__pv b,
html[data-theme="dark"] #tab-messages .chat-dlg__sub,
html[data-theme="dark"] #tab-messages .chat-messenger-note,
html[data-theme="dark"] #tab-messages .chat-readonly {
    color: var(--text-secondary);
}
html[data-theme="dark"] #tab-messages :is(.chat-row__at, .chat-row__pv--muted, .chat-daysep, .chat-sys, .chat-messenger-target small) {
    color: var(--muted);
}
html[data-theme="dark"] #tab-messages .chat-dlg__head,
html[data-theme="dark"] #tab-messages .chat-messenger-linkbox {
    border-color: var(--border);
}
html[data-theme="dark"] #tab-messages .chat-row:hover,
html[data-theme="dark"] #tab-messages .chat-row--pin,
html[data-theme="dark"] #tab-messages .chat-row--pin:hover,
html[data-theme="dark"] #tab-messages .chat-row.is-selected,
html[data-theme="dark"] #tab-messages .chat-row.is-selected:hover,
html[data-theme="dark"] #tab-messages .chat-readonly {
    background-color: var(--surface-hover);
}
html[data-theme="dark"] #tab-messages .chat-daysep::before,
html[data-theme="dark"] #tab-messages .chat-daysep::after {
    background: var(--border);
}
html[data-theme="dark"] #tab-messages .chat-dlg__more {
    background: var(--surface-2);
    color: var(--muted);
}
html[data-theme="dark"] #tab-messages .chat-dlg__chip {
    background: var(--primary-soft);
    color: var(--primary-ink);
}

html[data-theme="dark"] #tab-settings .notification-channel,
html[data-theme="dark"] #tab-settings .notification-preference {
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-settings .notification-channel__state.is-connected {
    color: var(--success-ink);
}
html[data-theme="dark"] #tab-settings .notification-channel__state.is-warning {
    color: var(--warning-ink);
}

html[data-theme="dark"] #topupOverlay .clarify-modal {
    background: var(--card);
    color: var(--text);
}
html[data-theme="dark"] #topupOverlay .clarify-modal__head {
    border-color: var(--border);
}
html[data-theme="dark"] #topupOverlay :is(.clarify-modal__progress, .clarify-modal__close) {
    color: var(--text-secondary);
}
html[data-theme="dark"] #billingModalScreen .crm-drawer {
    background: var(--bg);
    color: var(--text);
}

/* More dark-theme coverage for nested client, task, and entity-form components. */
html[data-theme="dark"] #tab-tasks .task__detail-head {
    background-color: var(--card);
}
html[data-theme="dark"] #tab-tasks .por-btn-danger {
    background-color: var(--card);
    border-color: var(--danger-border);
    color: var(--danger-ink);
}
html[data-theme="dark"] #tab-tasks .por-btn-danger:hover {
    background-color: var(--danger-bg);
}

html[data-theme="dark"] #tab-clients .crm-entity-table-wrap {
    background-color: var(--card);
}
html[data-theme="dark"] #tab-clients .crm-entity-th {
    background-color: var(--surface-2);
    color: var(--muted);
}
html[data-theme="dark"] #tab-clients .crm-entity-td {
    background-color: var(--card);
    color: var(--text);
}
html[data-theme="dark"] #tab-clients .crm-entity-row:hover .crm-entity-td {
    background-color: var(--surface-hover);
}
html[data-theme="dark"] #tab-clients .crm-entity-deleted {
    background-color: var(--danger-bg);
    color: var(--danger-ink);
}

html[data-theme="dark"] #tab-entities .ent-dialog :is(input, select, textarea),
html[data-theme="dark"] .ent-picker :is(input, select, textarea) {
    color-scheme: dark;
    background-color: var(--card);
    border-color: var(--border);
    color: var(--text);
}
html[data-theme="dark"] #tab-entities .ent-dialog :is(input, textarea)::placeholder,
html[data-theme="dark"] .ent-picker :is(input, textarea)::placeholder {
    color: var(--muted);
}
html[data-theme="dark"] #tab-entities .ent-history-table th {
    background-color: var(--surface-2);
    color: var(--muted);
}
