/* ============================================================
   CRM dark mode.
   Every color the stylesheets use is a token named for the color and the job it does there
   (crm-theme.tokens.css holds the light values, identical to the old literals). Dark mode
   gives those tokens new values on the CRM's page body only: <main class="crm-area"> is set
   by MainLayout on CRM routes, and <html data-theme="dark"> by js/crm-theme.js. The public
   pages, the header and the footer keep the brand look in both modes.
   The same color can go two ways: white is a card background (dark here) but also the text
   on green buttons (still white); forest green is a button background (kept) but also
   heading text (lightened to read on dark).
   New styles: use an existing --crm-* token for the job (e.g. --crm-c6b6b64-text for muted
   text, --crm-cffffff-bg for a card), or add one to crm-theme.tokens.css with its dark value
   here. A literal color in CRM CSS stays the same color in dark mode.
   ============================================================ */

html[data-theme="dark"] main.crm-area {
    color-scheme: dark;

    /* Surfaces */
    --crm-page: #141612;
    --crm-surface: #1c1f1a;
    --crm-surface-alt: #242820;
    --crm-raised: #2d3229;
    --crm-divider: #353a31;
    --crm-text: #e9e5da;
    --crm-text-2: #cfcabe;
    --crm-text-muted: #a9a59a;
    --crm-heading: #a9d8b8;
    --crm-link: #8fd0a5;

    background: var(--crm-page);
    color: var(--crm-text);

    /* Bootstrap 5.3 reads these for form fields, selects, modals, tables and dropdowns. */
    --bs-body-bg: var(--crm-surface);
    --bs-body-color: var(--crm-text);
    --bs-emphasis-color: #fff;
    --bs-secondary-color: rgba(233, 229, 218, .7);
    --bs-secondary-bg: var(--crm-surface-alt);
    --bs-tertiary-bg: var(--crm-raised);
    --bs-border-color: #4a4f45;
    --bs-border-color-translucent: rgba(233, 229, 218, .15);
    --bs-heading-color: var(--crm-heading);
    --bs-link-color-rgb: 143, 208, 165;
    --bs-form-select-bg-img: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23dee2e6' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e");

    /* Shared shadows: black, not forest green, on a dark page. */
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
    --shadow-sm: 0 4px 14px rgba(0, 0, 0, .35);

    /* ---- Text ---- */
    --crm-ink-text: var(--crm-text);
    --crm-c0b0b0b-text: var(--crm-text);
    --crm-c3a3a35-text: var(--crm-text);
    --crm-c3b3b36-text: var(--crm-text);
    --crm-c3d3d38-text: var(--crm-text);
    --crm-c4a4a44-text: var(--crm-text-2);
    --crm-c52514e-text: var(--crm-text-2);
    --crm-c6b6b64-text: var(--crm-text-muted);
    --crm-c898781-text: var(--crm-text-muted);
    --crm-c8a8a82-text: var(--crm-text-muted);
    --crm-forest-text: var(--crm-heading);
    --crm-green-text: var(--crm-link);
    --crm-c1f3d2b-text: var(--crm-heading);
    --crm-c1b7a48-text: #7fd6a0;
    --crm-c2e6b3f-text: #7fd6a0;
    --crm-gold-dark-text: #d9b65c;
    --crm-c6b4e00-text: #e3c062;
    --crm-c7a5a12-text: #e3c062;
    --crm-c8a5a00-text: #e3c062;
    --crm-c8a6110-text: #e3c062;
    --crm-c8a6d12-text: #e3c062;
    --crm-c9c3b2c-text: #ec8a78;
    --crm-c7f2f22-text: #f0a294;
    --crm-cb42318-text: #f08070;
    --crm-rgb11-11-11-text: 233, 229, 218;
    --crm-rgb31-61-43-text: 233, 229, 218;
    /* Kept: white and cream text sit on green or brick buttons, pills and banners. */

    /* ---- Backgrounds ---- */
    --crm-cffffff-bg: var(--crm-surface);
    --crm-cfbf9f4-bg: var(--crm-surface-alt);
    --crm-cf4efe3-bg: var(--crm-surface-alt);
    --crm-cream-bg: var(--crm-surface-alt);
    --crm-cfff8e8-bg: #3a311c;
    --crm-cf3e6c7-bg: #3a311c;
    --crm-cfdecc8-bg: #3a311c;
    --crm-cddebdd-bg: #1e3326;
    --crm-cf2dad5-bg: #3d2420;
    --crm-cc9c9c1-bg: #5a5d55;
    --crm-c1a1a19-bg: #3a3f37;
    --crm-ink-bg: #3a3f37;
    --crm-rgb239-233-220-bg: 60, 64, 55;
    --crm-rgb31-61-43-bg: 233, 229, 218;
    --crm-rgb43-43-40-bg: 233, 229, 218;
    --crm-rgb107-107-100-bg: 169, 165, 154;
    --crm-rgb58-107-76-bg: 127, 181, 146;
    --crm-rgb156-59-44-bg: 236, 138, 120;
    /* Kept: forest, green, gold, brick and gold-tint backgrounds (buttons, headers, pills). */

    /* ---- Lines ---- */
    --crm-ce1e0d9-line: var(--crm-divider);
    --crm-ce6e0d2-line: var(--crm-divider);
    --crm-cefeae0-line: var(--crm-divider);
    --crm-cream-line: var(--crm-divider);
    --crm-cc3c2b7-line: #4a4e45;
    --crm-c9a9a92-line: #6a6d64;
    --crm-c2b2b28-line: #c9c6bd;
    --crm-forest-line: #6f9f80;
    --crm-green-line: #6f9f80;
    --crm-rgb31-61-43-line: 233, 229, 218;
    --crm-rgb11-11-11-line: 233, 229, 218;
    --crm-rgb43-43-40-line: 233, 229, 218;
    --crm-rgb239-233-220-line: 90, 96, 84;
    --crm-rgb58-107-76-line: 127, 181, 146;
    --crm-rgb156-59-44-line: 236, 138, 120;

    /* ---- Shadows ---- */
    --crm-cffffff-shadow: var(--crm-surface);
    --crm-rgb58-107-76-shadow: 127, 181, 146;
    --crm-rgb11-11-11-shadow: 0, 0, 0;
}

/* Chart and funnel palettes keep their series colors; their text and gridlines go light. */
html[data-theme="dark"] main.crm-area .kpi-chart-block,
html[data-theme="dark"] main.crm-area .funnel-block {
    --ink-secondary: #cfcabe;
    --ink-muted: #a9a59a;
    --gridline: #353a31;
    --baseline: #5a5e55;
}

/* Bootstrap's close button is a dark glyph; lighten it as Bootstrap's own dark theme does. */
html[data-theme="dark"] main.crm-area .btn-close {
    filter: invert(1) grayscale(100%) brightness(200%);
}

/* Map: darken the street tiles, and give popups the dark surface. */
html[data-theme="dark"] main.crm-area .leaflet-tile-pane {
    filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9);
}

html[data-theme="dark"] main.crm-area .leaflet-container {
    background: #1c1f1a;
}

html[data-theme="dark"] main.crm-area .leaflet-popup-content-wrapper,
html[data-theme="dark"] main.crm-area .leaflet-popup-tip {
    background: var(--crm-surface);
    color: var(--crm-text);
}

html[data-theme="dark"] main.crm-area .leaflet-control-zoom a,
html[data-theme="dark"] main.crm-area .leaflet-control-attribution {
    background: var(--crm-surface-alt);
    color: var(--crm-text-2);
}

/* ---------- The toggle in the nav (green header in both modes) ---------- */
.theme-toggle {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    background: none;
    border: 1px solid rgba(239, 233, 220, .35);
    border-radius: 999px;
    padding: .2rem .7rem;
    font: inherit;
    font-size: .85rem;
    color: rgba(239, 233, 220, .9);
    cursor: pointer;
}

.theme-toggle:hover,
.theme-toggle:focus-visible {
    border-color: var(--gold);
    color: #fff;
}

.theme-toggle svg {
    width: 1rem;
    height: 1rem;
}
