/* Shared theme tokens and final overrides for component-scoped styles. */
:root {
    color-scheme: light;
    --spotter-bg: #f5f8fc;
    --spotter-surface: #fff;
    --spotter-surface-raised: #f4f7fb;
    --spotter-surface-hover: #eaf1f8;
    --spotter-border: #dce4ee;
    --spotter-text: #17304d;
    --spotter-muted: #647b96;
    --spotter-input: #fff;
}

body:has(.app-root.theme-dark) {
    color-scheme: dark;
    --spotter-bg: #111a28;
    --spotter-surface: #1b2839;
    --spotter-surface-raised: #223249;
    --spotter-surface-hover: #2a3b52;
    --spotter-border: #3b4d63;
    --spotter-text: #e7edf5;
    --spotter-muted: #a9bbd0;
    --spotter-input: #121d2c;
    background: var(--spotter-bg);
    color: var(--spotter-text);
}

/* Keep the dark palette consistent across page panels and Radzen surfaces. */
body:has(.app-root.theme-dark) .app-root,
body:has(.app-root.theme-dark) .app-area,
body:has(.app-root.theme-dark) .main {
    background-color: var(--spotter-bg);
    color: var(--spotter-text);
}

body:has(.app-root.theme-dark) :is(
    .panel, .stat, .company-card, .rz-card, .rz-card-body,
    .documentation-navigation, .documentation-content,
    .calendar-sidebar, .calendar-main, .tasks-sidebar,
    .tasks-filters, .tasks-list, .builder-section, .builder-preview,
    .content-record-dialog, .advanced-filter-row,
    .file-manager-card, .file-manager-card .rz-card-body
) {
    background-color: var(--spotter-surface) !important;
    color: var(--spotter-text);
    border-color: var(--spotter-border);
}

body:has(.app-root.theme-dark) :is(
    h1, h2, h3, h4, h5, label, th, strong,
    .page-title, .rz-tabview-nav a, .rz-tabview-nav li
) {
    color: var(--spotter-text);
}

body:has(.app-root.theme-dark) :is(
    .muted, small, .field-label, .page-subtitle,
    .tasks-context small, .documentation-footer, .collab-dates
) {
    color: var(--spotter-muted);
}

body:has(.app-root.theme-dark) :is(
    input:not([type="checkbox"]):not([type="radio"]), textarea, select,
    .rz-textbox, .rz-inputtext, .rz-dropdown, .rz-multiselect,
    .rz-numeric, .rz-datepicker, .rz-autocomplete,
    .rz-html-editor-content, .rz-html-editor-content iframe
) {
    background-color: var(--spotter-input);
    color: var(--spotter-text);
    border-color: var(--spotter-border);
}

body:has(.app-root.theme-dark) :is(input, textarea, select)::placeholder {
    color: #91a4bb;
    opacity: 1;
}

/* Tables use one readable surface in native HTML, Radzen grids and calendars. */
body:has(.app-root.theme-dark) :is(table, .rz-grid-table, .rz-data-grid, .rz-datatable) {
    background: var(--spotter-surface) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

body:has(.app-root.theme-dark) :is(table, .rz-grid-table) :is(thead, th) {
    background: var(--spotter-surface-raised) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

body:has(.app-root.theme-dark) :is(table, .rz-grid-table) :is(tbody, tr, td) {
    background-color: var(--spotter-surface) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

body:has(.app-root.theme-dark) :is(table, .rz-grid-table) tbody tr:nth-child(even) td {
    background-color: var(--spotter-surface-raised) !important;
}

body:has(.app-root.theme-dark) :is(table, .rz-grid-table) tbody tr:hover td {
    background-color: var(--spotter-surface-hover) !important;
}

/* Custom editors that intentionally define light surfaces in scoped CSS. */
body:has(.app-root.theme-dark) .sc-designer :is(.sc-designer-zone, .sc-designer-type, .sc-designer-field),
body:has(.app-root.theme-dark) .content-kanban-column,
body:has(.app-root.theme-dark) .content-kanban-card,
body:has(.app-root.theme-dark) .chat-table-wrap,
body:has(.app-root.theme-dark) .chat-result-table th {
    background-color: var(--spotter-surface-raised) !important;
    color: var(--spotter-text);
    border-color: var(--spotter-border);
}

body:has(.app-root.theme-dark) :is(.content-kanban-card-title, .content-calendar-appointment-title, .content-record-action) {
    color: var(--spotter-text);
}

body:has(.app-root.theme-dark) .tasks-kanban-column {
    background-color: var(--spotter-surface-raised);
    border-color: var(--spotter-border);
}

body:has(.app-root.theme-dark) .tasks-kanban-card {
    background-color: var(--spotter-surface);
    color: var(--spotter-text);
    border-color: var(--spotter-border);
}

/* Preserve semantic task status colors while keeping their text legible. */
body:has(.app-root.theme-dark) .status-nuovo { color: #bae6fd; background: #12364a; }
body:has(.app-root.theme-dark) .status-in-lavorazione { color: #fde68a; background: #453716; }
body:has(.app-root.theme-dark) .status-test { color: #e9d5ff; background: #3b2455; }
body:has(.app-root.theme-dark) .status-sospeso { color: #fed7aa; background: #4a2b19; }
body:has(.app-root.theme-dark) .status-chiuso { color: #bbf7d0; background: #173b2a; }

/* File Manager tree, breadcrumbs and content area. */
body:has(.app-root.theme-dark) .file-manager-card :is(.rz-tree, .rz-data-grid, .rz-grid-table, .rz-datatable) {
    background: var(--spotter-surface) !important;
    color: var(--spotter-text) !important;
}

body:has(.app-root.theme-dark) .file-manager-card .rz-treenode-content {
    color: var(--spotter-text);
}

body:has(.app-root.theme-dark) .file-manager-card .rz-treenode-content:is(:hover, .rz-state-highlight, .rz-state-focused) {
    background-color: var(--spotter-surface-hover);
    color: var(--spotter-text);
}

body:has(.app-root.theme-dark) .file-manager-card .file-manager-name-button {
    color: var(--spotter-text);
}

/* Radzen dialogs, menus, tooltips and popups render outside the app root. */
body:has(.app-root.theme-dark) :is(
    .rz-dialog, .rz-dialog-content, .rz-dialog-titlebar, .rz-popup,
    .rz-dropdown-panel, .rz-multiselect-panel, .rz-datepicker-calendar,
    .rz-autocomplete-panel, .rz-notification, .rz-tooltip,
    .rz-scheduler, .rz-scheduler-header, .rz-scheduler-view
) {
    background-color: var(--spotter-surface) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

body:has(.app-root.theme-dark) :is(.rz-dialog, .rz-popup, .rz-dropdown-panel, .rz-multiselect-panel, .rz-datepicker-calendar, .rz-autocomplete-panel) :is(input, textarea, select, .rz-inputtext, .rz-dropdown, .rz-multiselect) {
    background-color: var(--spotter-input) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

/* Admin session audit cards had fixed white surfaces in app.css, which made
   their light text unreadable when the rest of the application was dark. */
.admin-session-detail-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: none;
}

body:has(.app-root.theme-dark) .admin-session-dialog :is(.admin-chat-line, .admin-run-card, .admin-approval-card) {
    background-color: var(--spotter-surface) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

body:has(.app-root.theme-dark) .admin-session-dialog .admin-chat-line.user {
    background-color: var(--spotter-surface-raised) !important;
}

body:has(.app-root.theme-dark) .admin-session-dialog :is(.admin-tool-item, .rz-tabview-panels, .rz-tabview-panel) {
    background-color: var(--spotter-surface) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

body:has(.app-root.theme-dark) .admin-session-dialog :is(pre, .admin-tool-body) {
    background-color: var(--spotter-input) !important;
    color: var(--spotter-text) !important;
    border-color: var(--spotter-border) !important;
}

body:has(.app-root.theme-dark) .admin-session-dialog :is(.admin-run-kind, .admin-tool-item summary span) {
    background-color: var(--spotter-surface-hover) !important;
    color: var(--spotter-text) !important;
}

body:has(.app-root.theme-dark) .admin-session-dialog :is(.admin-chat-meta time, .admin-run-card time, .admin-approval-card time, .admin-tool-item time) {
    color: var(--spotter-muted) !important;
}

/* The company picker lives in the dark sidebar and its dropdown popup is
   rendered at document level by Radzen, outside the themed app subtree. */
body:has(.app-root.theme-dark) .active-company {
    background-color: #29415e;
    color: #f3f7fc;
}

body:has(.app-root.theme-dark) .active-company .active-company-dropdown,
body:has(.app-root.theme-dark) .active-company .active-company-dropdown:is(:hover, :focus, .rz-state-focused) {
    background-color: #29415e !important;
    color: #f3f7fc !important;
    border-color: #3b5879 !important;
}

body:has(.app-root.theme-dark) .active-company .active-company-dropdown :is(.rz-dropdown-label, .rz-dropdown-trigger, .rzi) {
    color: #f3f7fc !important;
}

body:has(.app-root.theme-dark) .rz-dropdown-panel :is(.rz-dropdown-item, .rz-multiselect-item) {
    background-color: var(--spotter-surface) !important;
    color: var(--spotter-text) !important;
}

body:has(.app-root.theme-dark) .rz-dropdown-panel :is(.rz-dropdown-item:hover, .rz-dropdown-item.rz-state-focused, .rz-dropdown-item.rz-state-highlight, .rz-multiselect-item:hover) {
    background-color: var(--spotter-surface-hover) !important;
    color: #fff !important;
}

body:has(.app-root.theme-dark) .active-company ::selection {
    background: #42658d;
    color: #fff;
}

@media (prefers-reduced-motion: no-preference) {
    .app-root, .app-root :is(.panel, .rz-card, .file-manager-card) {
        transition: background-color .15s ease, color .15s ease, border-color .15s ease;
    }
}
