/* ═══ CRM Theme — dark mode token overrides ═══
   All components use var(--crm-*) from app.css :root.
   Dark theme only redefines tokens; no per-component patches needed. */

html.theme-dark {
    color-scheme: dark;

    --crm-brand: #818cf8;
    --crm-brand-hover: #6366f1;
    --crm-brand-soft: rgba(99, 102, 241, 0.16);
    --crm-brand-ring: rgba(129, 140, 248, 0.24);

    --crm-surface: #0c1017;
    --crm-card: #151b26;
    --crm-elevated: #1c2433;

    --crm-border: #2a3344;
    --crm-border-strong: #3d4a5f;
    --crm-border-subtle: #222b3c;
    --crm-border-light: #2a3344;
    --crm-border-faint: #1f2836;
    --crm-brand-border: rgba(129, 140, 248, 0.35);
    --crm-brand-muted: rgba(99, 102, 241, 0.22);
    --crm-accent: #a5b4fc;
    --crm-accent-light: #818cf8;
    --crm-violet-soft: rgba(124, 58, 237, 0.16);
    --crm-blue-soft: rgba(37, 99, 235, 0.16);
    --crm-teal-soft: rgba(13, 148, 136, 0.16);
    --crm-amber-soft: rgba(245, 158, 11, 0.16);

    --crm-text: #e2e8f0;
    --crm-text-secondary: #cbd5e1;
    --crm-muted: #94a3b8;
    --crm-muted-light: #64748b;

    --crm-input-bg: #101622;
    --crm-hover: #1c2433;
    --crm-hover-strong: #222b3c;
    --crm-hover-soft: #1a2230;

    --crm-nav-active-bg: rgba(99, 102, 241, 0.18);
    --crm-nav-active-text: #a5b4fc;

    --crm-overlay: rgba(0, 0, 0, 0.62);
    --crm-shadow: 0 1px 3px rgba(0, 0, 0, 0.35), 0 8px 24px rgba(0, 0, 0, 0.28);
}

html.theme-dark body {
    background-color: var(--crm-surface) !important;
    color: var(--crm-text) !important;
}

/* Tailwind CDN utility fallbacks loaded before theme */
html.theme-dark .bg-slate-50 { background-color: var(--crm-surface) !important; }
html.theme-dark .text-slate-800,
html.theme-dark .text-slate-900 { color: var(--crm-text) !important; }
html.theme-dark .text-slate-600,
html.theme-dark .text-slate-500,
html.theme-dark .text-slate-700 { color: var(--crm-muted) !important; }
html.theme-dark .border-slate-200,
html.theme-dark .border-slate-100 { border-color: var(--crm-border) !important; }
html.theme-dark .bg-white { background-color: var(--crm-card) !important; }

/* Semantic status badges — slightly dimmed backgrounds in dark */
html.theme-dark .crm-badge-blue { background: rgba(59, 130, 246, 0.18); color: #93c5fd; }
html.theme-dark .crm-badge-sky { background: rgba(14, 165, 233, 0.18); color: #7dd3fc; }
html.theme-dark .crm-badge-amber { background: rgba(245, 158, 11, 0.18); color: #fcd34d; }
html.theme-dark .crm-badge-green { background: rgba(34, 197, 94, 0.18); color: #86efac; }
html.theme-dark .crm-badge-red { background: rgba(239, 68, 68, 0.18); color: #fca5a5; }
html.theme-dark .crm-badge-slate { background: var(--crm-hover-strong); color: var(--crm-muted); }

html.theme-dark .crm-alert-error {
    background: rgba(127, 29, 29, 0.28);
    border-color: #991b1b;
    color: #fca5a5;
}
html.theme-dark .crm-alert-success {
    background: rgba(21, 128, 61, 0.22);
    border-color: #166534;
    color: #86efac;
}

html.theme-dark .crm-flash {
    background: var(--crm-card);
    box-shadow:
        0 4px 6px -1px rgba(0, 0, 0, 0.25),
        0 16px 32px -8px rgba(0, 0, 0, 0.45);
}
html.theme-dark .crm-flash--success {
    border-color: rgba(52, 211, 153, 0.35);
    background: linear-gradient(135deg, var(--crm-card) 0%, rgba(6, 78, 59, 0.45) 100%);
}
html.theme-dark .crm-flash--error {
    border-color: rgba(248, 113, 113, 0.35);
    background: linear-gradient(135deg, var(--crm-card) 0%, rgba(127, 29, 29, 0.35) 100%);
}
html.theme-dark .crm-flash--success .crm-flash__label { color: #6ee7b7; }
html.theme-dark .crm-flash--error .crm-flash__label { color: #fca5a5; }
html.theme-dark .crm-flash--success .crm-flash__icon {
    background: rgba(16, 185, 129, 0.2);
    color: #34d399;
}
html.theme-dark .crm-flash--error .crm-flash__icon {
    background: rgba(239, 68, 68, 0.2);
    color: #f87171;
}
html.theme-dark .client-toast.is-success {
    background: linear-gradient(135deg, var(--crm-card) 0%, rgba(6, 78, 59, 0.5) 100%);
    border-color: rgba(52, 211, 153, 0.35);
}
html.theme-dark .client-toast.is-error {
    background: linear-gradient(135deg, var(--crm-card) 0%, rgba(127, 29, 29, 0.4) 100%);
    border-color: rgba(248, 113, 113, 0.35);
}

/* Impersonation banner */
html.theme-dark .crm-impersonation-banner {
    background: linear-gradient(90deg, #422006 0%, #451a03 100%);
    border-bottom-color: #92400e;
    color: #fcd34d;
}
html.theme-dark .crm-impersonation-banner__text strong { color: #fef3c7; }
html.theme-dark .crm-impersonation-banner__btn {
    background: var(--crm-card);
    border-color: #d97706;
    color: #fcd34d;
}

/* Locked module overlay */
html.theme-dark .module-card-wrap__veil { background: rgba(12, 16, 23, 0.5); }
html.theme-dark .module-card-wrap__lock {
    background: var(--crm-card);
    border-color: #92400e;
    color: #fcd34d;
}

/* Login hero panel stays dark gradient — intentional brand panel */
html.theme-dark .bg-surface { background-color: var(--crm-surface) !important; }

/* Scrollbars */
html.theme-dark ::-webkit-scrollbar { width: 10px; height: 10px; }
html.theme-dark ::-webkit-scrollbar-track { background: var(--crm-surface); }
html.theme-dark ::-webkit-scrollbar-thumb {
    background: var(--crm-border-strong);
    border-radius: 999px;
    border: 2px solid var(--crm-surface);
}
html.theme-dark ::-webkit-scrollbar-thumb:hover { background: #475569; }

/* Shadows tuned for dark surfaces */
html.theme-dark .clients-stat-card:hover,
html.theme-dark .crm-stat-card:hover,
html.theme-dark .cw-btn:hover:not(:disabled) {
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
}

html.theme-dark .icon-btn:hover:not(:disabled) {
    background: var(--crm-hover);
    color: var(--crm-text);
}

html.theme-dark .icon-btn-primary:hover:not(:disabled) {
    background: rgba(59, 130, 246, 0.15);
    border-color: rgba(96, 165, 250, 0.35);
    color: #93c5fd;
}

html.theme-dark .icon-btn-danger:hover:not(:disabled) {
    background: rgba(239, 68, 68, 0.15);
    border-color: rgba(248, 113, 113, 0.35);
    color: #fca5a5;
}

html.theme-dark .icon-btn-success:hover:not(:disabled) {
    background: rgba(34, 197, 94, 0.15);
    border-color: rgba(74, 222, 128, 0.35);
    color: #86efac;
}

html.theme-dark .cw-modal.is-open,
html.theme-dark .crm-modal-overlay.is-open {
    background: var(--crm-overlay);
}

html.theme-dark .cw-modal__backdrop,
html.theme-dark .crm-modal-backdrop {
    background: rgba(0, 0, 0, 0.55);
}

/* ── Page surfaces (dashboard, tasks, tickets, chats, calendar, users, services, roles) ── */
html.theme-dark .dashboard__hero,
html.theme-dark .dashboard__stat,
html.theme-dark .dashboard__panel,
html.theme-dark .tasks-workspace,
html.theme-dark .tasks-stat-card,
html.theme-dark .tasks-table-wrap,
html.theme-dark .it-toolbar,
html.theme-dark .it-stat-card,
html.theme-dark .it-table-wrap,
html.theme-dark .it-show-layout,
html.theme-dark .it-sidebar,
html.theme-dark .sc-layout,
html.theme-dark .sc-list,
html.theme-dark .sc-thread,
html.theme-dark .cal-workspace,
html.theme-dark .cal-hero,
html.theme-dark .users-workspace,
html.theme-dark .svc-settings,
html.theme-dark .svc-panel,
html.theme-dark .roles-workspace {
    background-color: var(--crm-card);
    border-color: var(--crm-border);
    color: var(--crm-text);
}

html.theme-dark .sc-list,
html.theme-dark .svc-settings__sidebar,
html.theme-dark .roles-workspace__sidebar {
    background: var(--crm-card);
    border-color: var(--crm-border);
}

html.theme-dark .dashboard__hero,
html.theme-dark .cal-hero,
html.theme-dark .tasks-workspace__hero,
html.theme-dark .it-toolbar {
    background: linear-gradient(135deg, var(--crm-card) 0%, var(--crm-surface) 55%, var(--crm-brand-soft) 100%);
}

html.theme-dark .sc-conv,
html.theme-dark .sc-conv:hover {
    background: var(--crm-card);
    border-color: var(--crm-border-faint);
}

html.theme-dark .sc-conv.is-active {
    background: linear-gradient(135deg, var(--crm-brand-soft) 0%, rgba(99, 102, 241, 0.08) 100%);
    border-color: var(--crm-brand-border);
}

html.theme-dark .sc-thread__messages,
html.theme-dark .sc-compose,
html.theme-dark .it-show-main,
html.theme-dark .roles-workspace__main {
    background: var(--crm-surface);
}

html.theme-dark .tasks-table__row:hover,
html.theme-dark .it-ticket-row:hover,
html.theme-dark .dashboard__task-item:hover {
    background: var(--crm-hover);
}

/* Semantic status pills — lighter text in dark */
html.theme-dark .dashboard__badge--progress,
html.theme-dark .it-status--open,
html.theme-dark .it-priority--normal,
html.theme-dark .sc-status--open,
html.theme-dark .tasks-status--progress,
html.theme-dark .tasks-priority--medium { color: #93c5fd; }

html.theme-dark .dashboard__badge--overdue,
html.theme-dark .dashboard__badge--priority-urgent,
html.theme-dark .it-status--urgent,
html.theme-dark .it-priority--urgent,
html.theme-dark .sc-status--waiting-agent,
html.theme-dark .tasks-priority--urgent,
html.theme-dark .tasks-cell-due--overdue .tasks-cell-due__date { color: #fca5a5; }

html.theme-dark .dashboard__badge--due-today,
html.theme-dark .dashboard__badge--priority-high,
html.theme-dark .it-status--waiting-user,
html.theme-dark .it-priority--high,
html.theme-dark .sc-status--waiting-client,
html.theme-dark .tasks-priority--high { color: #fcd34d; }

html.theme-dark .it-status--resolved,
html.theme-dark .it-status--progress,
html.theme-dark .sc-status--resolved,
html.theme-dark .tasks-status--done { color: #86efac; }

html.theme-dark .it-cat-pill[style*="blue"],
html.theme-dark .it-cat-pill[style*="violet"],
html.theme-dark .it-cat-pill[style*="amber"],
html.theme-dark .it-cat-pill[style*="green"],
html.theme-dark .it-cat-pill[style*="red"],
html.theme-dark .it-cat-pill[style*="orange"] {
    filter: brightness(1.15);
}

html.theme-dark .roles-workspace__sidebar-item-wrap.is-active {
    background: var(--crm-nav-active-bg);
}

html.theme-dark .roles-workspace__sidebar-item-wrap.is-active .roles-workspace__sidebar-item-name {
    color: var(--crm-nav-active-text);
}

html.theme-dark .svc-nav-card:hover,
html.theme-dark .svc-settings__nav-item:hover {
    background: var(--crm-hover);
}

html.theme-dark .cal-workspace .cal-day-cell.is-other-month {
    background: var(--crm-surface);
    opacity: 0.55;
}

html.theme-dark .cal-workspace .cal-day-cell.is-today {
    background: var(--crm-brand-soft);
}

html.theme-dark .users-workspace__avatar-ring {
    border-color: var(--crm-card);
}
