/*
 * Dark mode, following the operating system's preference
 * (prefers-color-scheme). Light mode is untouched: everything below that
 * changes a colour sits inside the media query, so the CF look is identical
 * for light-mode users.
 *
 * Loaded twice, like the themes it overrides:
 *   - by the page shells (<link>), for the page's own light DOM;
 *   - by components/_lib/theme.js, adopted into every shadow root after the
 *     SB Admin / Brand sheet, so the same overrides apply inside components.
 * Selectors use the same specificity as the Bootstrap/SB Admin rules they
 * override (and !important where those use it); later-loaded wins.
 *
 * Covers the Bootstrap 4 pieces the CF portal used (body, cards, tables
 * incl. table-dark, forms, buttons, alerts, list groups, modals, dropdowns,
 * nav tabs, pagination, text/background utilities, the SB Admin sidebar and
 * topbar) and the public Brand theme (clean-block, clean-form, navbar).
 * Components with their own colours (e.g. the legacy olive/orange ticket
 * tables) add dark variants in their local CSS under the same media query.
 */

:root,
:host {
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root,
  :host {
    --ma-dark-bg: #15161c;
    --ma-dark-surface: #1e1f27;
    --ma-dark-surface-2: #272934;
    --ma-dark-surface-3: #31333f;
    --ma-dark-border: #363949;
    --ma-dark-input-border: #464a5e;
    --ma-dark-text: #b4b6c4;
    --ma-dark-text-strong: #e3e4eb;
    --ma-dark-heading: #eceef4;
    --ma-dark-muted: #8d90a3;
    --ma-dark-link: #8ea7f2;
    --ma-dark-link-hover: #b8c8f7;
    --ma-dark-primary: #7f9cf0;
  }

  /* ---- page ------------------------------------------------------------ */
  body {
    background-color: var(--ma-dark-bg);
    color: var(--ma-dark-text);
  }
  #wrapper #content-wrapper {
    background-color: var(--ma-dark-bg);
  }
  a {
    color: var(--ma-dark-link);
  }
  a:hover {
    color: var(--ma-dark-link-hover);
  }
  hr {
    border-top-color: rgba(255, 255, 255, 0.12);
  }
  code {
    color: #f28cbd;
  }

  /* ---- SB Admin sidebar + topbar ---------------------------------------- */
  .bg-gradient-primary {
    background-color: #2f4aa0;
    background-image: linear-gradient(180deg, #3653ad 10%, #1c2e6e);
  }
  .topbar .topbar-divider {
    border-right-color: var(--ma-dark-border);
  }

  /* ---- backgrounds, borders, shadows ------------------------------------- */
  .bg-white {
    background-color: var(--ma-dark-surface) !important;
  }
  .bg-light,
  .bg-gray-100 {
    background-color: var(--ma-dark-surface-2) !important;
  }
  .border {
    border-color: var(--ma-dark-border) !important;
  }
  .border-top {
    border-top-color: var(--ma-dark-border) !important;
  }
  .border-bottom {
    border-bottom-color: var(--ma-dark-border) !important;
  }
  .border-right {
    border-right-color: var(--ma-dark-border) !important;
  }
  .shadow {
    box-shadow: 0 0.15rem 1.75rem 0 rgba(0, 0, 0, 0.5) !important;
  }
  .shadow-sm {
    box-shadow: 0 0.125rem 0.25rem 0 rgba(0, 0, 0, 0.5) !important;
  }
  .shadow-lg {
    box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.6) !important;
  }

  /* ---- text utilities ---------------------------------------------------- */
  .text-dark,
  .text-gray-800 {
    color: var(--ma-dark-text-strong) !important;
  }
  .text-gray-900 {
    color: var(--ma-dark-heading) !important;
  }
  .text-gray-700 {
    color: #c8cad5 !important;
  }
  .text-gray-600,
  .text-muted,
  .text-body {
    color: var(--ma-dark-text) !important;
  }
  .text-gray-500 {
    color: #9a9daf !important;
  }
  .text-gray-400 {
    color: #6f7285 !important;
  }
  /* Decorative icons (e.g. the $ on the balance card) stay subtle. */
  .text-gray-300 {
    color: #4d5063 !important;
  }
  .text-gray-200 {
    color: #3d4050 !important;
  }
  .text-gray-100 {
    color: #2c2e3a !important;
  }
  .text-black-50 {
    color: rgba(255, 255, 255, 0.5) !important;
  }
  /* Brand colours as text, lifted for contrast on dark surfaces (AA). */
  .text-primary {
    color: var(--ma-dark-primary) !important;
  }
  .text-danger {
    color: #f0776a !important;
  }

  /* ---- cards, list groups, modals, dropdowns ----------------------------- */
  .card,
  .list-group-item,
  .modal-content,
  .dropdown-menu {
    background-color: var(--ma-dark-surface);
    border-color: var(--ma-dark-border);
    color: var(--ma-dark-text);
  }
  .card-header,
  .card-footer {
    background-color: var(--ma-dark-surface-2);
    border-color: var(--ma-dark-border);
  }
  .modal-header,
  .modal-footer,
  .dropdown-divider {
    border-color: var(--ma-dark-border);
  }
  .dropdown-item {
    color: var(--ma-dark-text);
  }
  .dropdown-item:focus,
  .dropdown-item:hover {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface-2);
  }
  .close {
    color: #fff;
    text-shadow: none;
  }

  /* ---- tables ------------------------------------------------------------ */
  .table {
    color: var(--ma-dark-text);
  }
  .table td,
  .table th,
  .table thead th,
  .table tbody + tbody,
  .table-bordered,
  .table-bordered td,
  .table-bordered th {
    border-color: var(--ma-dark-border);
  }
  .table-striped tbody tr:nth-of-type(odd) {
    background-color: rgba(255, 255, 255, 0.04);
  }
  .table-hover tbody tr:hover {
    color: var(--ma-dark-text-strong);
    background-color: rgba(255, 255, 255, 0.07);
  }
  .table .thead-light th {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface-2);
    border-color: var(--ma-dark-border);
  }
  /* The CF pages use table-dark (SB Admin gray #5a5c69) for most data
     tables; on a dark page it becomes a raised surface instead. */
  .table-dark {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface-2);
  }
  .table-dark td,
  .table-dark th,
  .table-dark thead th {
    border-color: #3a3d50;
  }
  .table-dark.table-striped tbody tr:nth-of-type(odd) {
    background-color: rgba(255, 255, 255, 0.04);
  }
  .table-dark.table-hover tbody tr:hover {
    background-color: rgba(255, 255, 255, 0.08);
  }

  /* ---- forms ------------------------------------------------------------- */
  .form-control,
  .custom-select,
  .input-group-text,
  .custom-file-label {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface-2);
    border-color: var(--ma-dark-input-border);
  }
  .form-control:focus,
  .custom-select:focus {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface-2);
    border-color: #6f8de6;
    box-shadow: 0 0 0 0.2rem rgba(78, 115, 223, 0.35);
  }
  .form-control::placeholder {
    color: #7c7f92;
  }
  .form-control:disabled,
  .form-control[readonly],
  .custom-select:disabled {
    color: var(--ma-dark-muted);
    background-color: #1b1c23;
  }
  .custom-select {
    background: var(--ma-dark-surface-2)
      url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='4' height='5' viewBox='0 0 4 5'%3e%3cpath fill='%23b4b6c4' d='M2 0L0 2h4zm0 5L0 3h4z'/%3e%3c/svg%3e")
      no-repeat right 0.75rem center/8px 10px;
  }
  .custom-control-label::before {
    background-color: var(--ma-dark-surface-2);
    border-color: var(--ma-dark-input-border);
  }

  /* ---- buttons ------------------------------------------------------------ */
  .btn-light {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface-2);
    border-color: var(--ma-dark-surface-2);
  }
  .btn-light:hover {
    color: var(--ma-dark-heading);
    background-color: var(--ma-dark-surface-3);
    border-color: var(--ma-dark-surface-3);
  }
  .btn-link {
    color: var(--ma-dark-link);
  }
  .btn-link:hover {
    color: var(--ma-dark-link-hover);
  }

  /* ---- alerts, badges ----------------------------------------------------- */
  .alert-primary {
    color: #b8c8f7;
    background-color: rgba(78, 115, 223, 0.18);
    border-color: rgba(78, 115, 223, 0.4);
  }
  .alert-success {
    color: #8ee6c4;
    background-color: rgba(28, 200, 138, 0.14);
    border-color: rgba(28, 200, 138, 0.35);
  }
  .alert-info {
    color: #9ddfe9;
    background-color: rgba(54, 185, 204, 0.14);
    border-color: rgba(54, 185, 204, 0.35);
  }
  .alert-warning {
    color: #f8dc8a;
    background-color: rgba(246, 194, 62, 0.14);
    border-color: rgba(246, 194, 62, 0.35);
  }
  .alert-danger {
    color: #f3b0a8;
    background-color: rgba(231, 74, 59, 0.15);
    border-color: rgba(231, 74, 59, 0.38);
  }
  .badge-light {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface-3);
  }

  /* ---- navigation: tabs, pagination --------------------------------------- */
  .nav-tabs {
    border-bottom-color: var(--ma-dark-border);
  }
  .nav-tabs .nav-link.active,
  .nav-tabs .nav-item.show .nav-link {
    color: var(--ma-dark-text-strong);
    background-color: var(--ma-dark-surface);
    border-color: var(--ma-dark-border) var(--ma-dark-border) var(--ma-dark-surface);
  }
  .page-link {
    color: var(--ma-dark-link);
    background-color: var(--ma-dark-surface);
    border-color: var(--ma-dark-border);
  }
  .page-item.disabled .page-link {
    color: var(--ma-dark-muted);
    background-color: var(--ma-dark-surface);
    border-color: var(--ma-dark-border);
  }

  /* ---- public Brand theme (login pages) ----------------------------------- */
  .clean-block.dark {
    background-color: var(--ma-dark-bg);
  }
  .clean-block .block-content,
  .clean-block .content,
  .clean-block.clean-form form {
    background-color: var(--ma-dark-surface);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  }
  .clean-navbar.fixed-top,
  .clean-navbar.fixed-bottom {
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
  }
  .page-footer {
    background-color: var(--ma-dark-surface);
  }
  /* The CF login page's <font color="red"> notices and the sign-in messages. */
  font[color="red"],
  .login-error {
    color: #ff7a6e;
  }
  .login-notice {
    color: #5dd39e;
  }
}
