/*
 * Global light/dark theme + accessibility helpers.
 *
 * Loaded after each page's own stylesheet by the server-side HTML injector.
 * The project has two variable naming schemes (styles.css uses --bg/--card,
 * older screens use --bg-color/--card-bg), so both are mapped here.
 */

html[data-theme="dark"] {
    color-scheme: dark;

    --bg: #0b1220;
    --bg-color: #0b1220;
    --card: #111b2e;
    --card-bg: #111b2e;
    --surface: #111b2e;
    --text: #edf2f7;
    --text-color: #edf2f7;
    --muted: #9aa8bc;
    --text-muted: #9aa8bc;
    --border: #243149;
    --border-color: #243149;
    --primary: #3b82f6;
    --primary-color: #3b82f6;
    --primary2: #60a5fa;
    --secondary-color: #60a5fa;
    --primary-dark: #1d4ed8;
    --primary-light: #172554;
    --table-hover: #16233a;
    --th-bg: #0f1a2b;
    --sticky-bg: #111b2e;
    --shadow: 0 10px 30px rgba(0, 0, 0, .32);
    --success: #4ade80;
    --success-color: #4ade80;
    --success-light: #052e16;
    --warning: #fbbf24;
    --warning-color: #fbbf24;
    --warning-light: #451a03;
    --danger: #f87171;
    --danger-color: #f87171;
    --danger-light: #450a0a;
    --info: #38bdf8;
    --info-color: #38bdf8;
    --info-light: #082f49;
    --radius: 14px;
    --radius-sm: 8px;
}

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

/* Neutral surfaces. background-color is used instead of background so
   decorative gradients on the login screen and stat cards keep working. */
html[data-theme="dark"] .card,
html[data-theme="dark"] .panel,
html[data-theme="dark"] .box,
html[data-theme="dark"] .section,
html[data-theme="dark"] .table-card,
html[data-theme="dark"] .table-wrap,
html[data-theme="dark"] .table-container,
html[data-theme="dark"] .summary-card,
html[data-theme="dark"] .stat-card,
html[data-theme="dark"] .form-card,
html[data-theme="dark"] .login-card,
html[data-theme="dark"] .login-box,
html[data-theme="dark"] .modal-card,
html[data-theme="dark"] .modal-content,
html[data-theme="dark"] .dialog,
html[data-theme="dark"] .dropdown-menu,
html[data-theme="dark"] .crm-header {
    background-color: var(--card);
    border-color: var(--border);
    color: var(--text);
}

html[data-theme="dark"] input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
    background-color: #0f172a;
    color: var(--text);
    border-color: var(--border);
}

html[data-theme="dark"] input::placeholder,
html[data-theme="dark"] textarea::placeholder {
    color: #94a3b8;
}

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

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

html[data-theme="dark"] tbody td {
    border-color: var(--border);
}

html[data-theme="dark"] tbody tr:hover {
    background-color: var(--table-hover);
}

html[data-theme="dark"] .btn-light {
    background-color: #172033;
    color: var(--text);
    border-color: var(--border);
}

html[data-theme="dark"] .btn-light:hover {
    background-color: #1e293b;
}

/* Older standalone screens (AI monitoring, customer month/year summaries,
   statements, print-style tables) hard-code white surfaces. Override those
   without touching primary/status buttons or severity borders. */
html[data-theme="dark"] .summary-item,
html[data-theme="dark"] .headline,
html[data-theme="dark"] .calc span,
html[data-theme="dark"] .team-card,
html[data-theme="dark"] .metric,
html[data-theme="dark"] .evidence,
html[data-theme="dark"] .tag,
html[data-theme="dark"] .panel,
html[data-theme="dark"] .box,
html[data-theme="dark"] .picker select,
html[data-theme="dark"] .field input,
html[data-theme="dark"] .field select,
html[data-theme="dark"] .quick-days button,
html[data-theme="dark"] .period-card .pc-edit,
html[data-theme="dark"] [style*="background:#fff"],
html[data-theme="dark"] [style*="background: #fff"],
html[data-theme="dark"] [style*="background:white"],
html[data-theme="dark"] [style*="background: white"] {
    background-color: var(--card) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .btn:not(.primary):not(.btn-primary):not(.btn-danger):not(.btn-success):not(.send) {
    background-color: #172033;
    color: var(--text);
    border-color: var(--border);
}

html[data-theme="dark"] .btn-return,
html[data-theme="dark"] .btn-del {
    background-color: #172033 !important;
}

html[data-theme="dark"] .alert {
    background-color: var(--card);
    color: var(--text);
}

html[data-theme="dark"] .empty {
    color: var(--text-muted);
}

html[data-theme="dark"] .invoice-row {
    background-color: #0f1a2b !important;
}

html[data-theme="dark"] .payment-row {
    background-color: #0b2418 !important;
}

html[data-theme="dark"] .return-row {
    background-color: #2a1f0a !important;
}

html[data-theme="dark"] tbody tr.blocked-row {
    background-color: #2a1215 !important;
}

html[data-theme="dark"] tbody tr.blocked-row:hover {
    background-color: #35161a !important;
}

html[data-theme="dark"] tbody tr.has-held {
    background-color: #2a1f0a !important;
}

html[data-theme="dark"] .alert-item.warning {
    background-color: #2a1f0a !important;
    color: #fbbf24 !important;
    border-color: #78350f !important;
}

/* The unified-account screen and the visit/payment modals in customer.html
   hard-code light surfaces. Without these overrides the metric values are
   white text on a white box in dark mode, and the modal itself stays light. */
html[data-theme="dark"] .modal-box,
html[data-theme="dark"] .metric-box,
html[data-theme="dark"] .visit-total,
html[data-theme="dark"] .visit-preview,
html[data-theme="dark"] .status-normal,
html[data-theme="dark"] #payment-info {
    background-color: #0f1a2b !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .modal-box {
    border: 1px solid var(--border);
    box-shadow: 0 20px 60px rgba(0, 0, 0, .55);
}

html[data-theme="dark"] .metric-box {
    border: 1px solid var(--border);
}

html[data-theme="dark"] .metric-box .m-value {
    color: var(--text);
}

html[data-theme="dark"] .modal-close:hover {
    background-color: #1e293b;
}

html[data-theme="dark"] .score-circle {
    background: conic-gradient(
        var(--score-color, var(--primary)) calc(var(--score, 0) * 1%),
        var(--border) 0
    );
}

html[data-theme="dark"] .score-circle::before {
    background-color: var(--card);
}

html[data-theme="dark"] .visit-total span,
html[data-theme="dark"] .visit-preview-title {
    color: var(--text-muted);
}

html[data-theme="dark"] .visit-total.open {
    background-color: #2a1f0a !important;
    border-color: #78350f !important;
}

html[data-theme="dark"] .visit-total.open b {
    color: #fbbf24;
}

html[data-theme="dark"] .visit-total.due {
    background-color: #2a1215 !important;
    border-color: #7f1d1d !important;
}

html[data-theme="dark"] .visit-total.due b {
    color: #f87171;
}

html[data-theme="dark"] .visit-total.settled {
    background-color: #0b2418 !important;
    border-color: #14532d !important;
}

html[data-theme="dark"] .visit-total.settled b {
    color: #4ade80;
}

html[data-theme="dark"] .visit-preview-row.total {
    border-color: var(--border);
}

html[data-theme="dark"] .visit-preview-note {
    background-color: #2a1f0a;
    color: #fbbf24;
}

html[data-theme="dark"] .visit-preview-note.over {
    background-color: #2a1215;
    color: #f87171;
}

html[data-theme="dark"] .status-normal {
    background-color: #1e293b !important;
    color: #cbd5e1 !important;
}

html[data-theme="dark"] .period-card.overdue {
    background-color: #2a1215 !important;
    color: #f87171 !important;
    border-color: #7f1d1d !important;
}

html[data-theme="dark"] .insight-item:not(.priority-high):not(.priority-medium):not(.priority-low):not(.tone-risk):not(.tone-watch):not(.tone-good) {
    background-color: #0f1a2b;
}

html[data-theme="dark"] hr {
    border-color: var(--border);
}

html[data-theme="dark"] .text-muted,
html[data-theme="dark"] .muted {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .brand-logo {
    /* The logo has a white background of its own; keep it readable. */
    background-color: #fff;
}

/* ------------------------------------------------------------------------- */
/* Accessibility                                                             */
/* ------------------------------------------------------------------------- */

.sr-only {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

.skip-link {
    position: fixed;
    top: -120px;
    inset-inline-start: 16px;
    z-index: 2147483647;
    padding: 10px 16px;
    border-radius: 10px;
    background: #111827;
    color: #fff;
    font-weight: 700;
    text-decoration: none;
    box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
    transition: top .15s ease;
}

.skip-link:focus {
    top: 12px;
    outline: 3px solid #f59e0b;
    outline-offset: 2px;
}

:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
    outline: 3px solid #f59e0b !important;
    outline-offset: 2px !important;
}

/* The floating theme switch. Pages that already have their own button do not
   get this one (theme.js checks first), so there are never two controls. */
.crm-a11y-toggle {
    position: fixed;
    left: 18px;
    bottom: 88px;
    z-index: 9998;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 10px 14px;
    border: 1px solid var(--border-color, var(--border, #d9e2ef));
    border-radius: 999px;
    background: var(--card-bg, var(--card, #ffffff));
    color: var(--text-color, var(--text, #172033));
    font-family: inherit;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(15, 23, 42, .18);
    transition: transform .15s ease, box-shadow .15s ease;
}

.crm-a11y-toggle:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 28px rgba(15, 23, 42, .24);
}

.crm-a11y-toggle i {
    font-size: 14px;
}

@media (max-width: 640px) {
    .crm-a11y-toggle {
        left: 12px;
        bottom: 76px;
        padding: 9px 12px;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}

@media (forced-colors: active) {
    .crm-a11y-toggle,
    .skip-link {
        border: 2px solid ButtonText;
    }
}
