/* theme/app-shell.css — shared day/night theme for every page built on
 * scripts/layout.php (dashboard, accounts, invoices, expenses, admin).
 *
 * HOW IT WORKS
 * scripts/layout.php sets data-bs-theme="light|dark" on <html> from the OS
 * preference, before paint. Bootstrap 5.3 recolours its own components and its
 * theme-aware utilities off that attribute on its own. This file does the
 * remaining three jobs:
 *
 *   1. Publishes an --app-* token vocabulary for the hand-written page styles,
 *      so a page never needs a dark-mode rule of its own.
 *   2. Patches the Bootstrap utilities that are NOT theme-aware (.bg-light,
 *      .bg-white, .text-dark — these are fixed palette colours in 5.3, not
 *      semantic ones, so they stay light in dark mode).
 *   3. Carries the few semantic colour pairs Bootstrap has no token for.
 *
 * WHY THE NEUTRALS ARE ALIASES
 * --app-surface and friends point at Bootstrap's own --bs-body-bg /
 * --bs-tertiary-bg / --bs-border-color rather than hardcoded hex. Those already
 * flip with data-bs-theme, so the neutral ramp needs no dark override here and
 * can never drift out of step with Bootstrap's components sitting next to it.
 * Only the accents below need an explicit pair.
 *
 * ADDING A COLOUR to a page style: use a token from here. A raw hex in a page
 * will stay light-mode forever.
 *
 * DELIBERATELY NOT THEMED: the document-rendering pages — invoices/receipt.php,
 * invoices/invoice_view.php, invoices/quotation_view.php, invoices/templates.php
 * and mykhata/receipt.php. An invoice is paper: it must print and read as black
 * ink on white regardless of the operating system's theme. They keep their own
 * light styles on purpose; don't "fix" them.
 */

:root {
    /* Page canvas. assets/styles/default.css hardcoded these as #f8f9fa and
       `color:#000 !important`, and it is re-included INSIDE <body> — i.e. after
       this file — so in dark mode the surfaces went dark while the text stayed
       forced black. It now reads these two tokens instead. Light values are
       byte-identical to what it had, so day mode is unchanged. */
    --app-page-bg:  #f8f9fa;
    --app-page-ink: #000000;

    /* Neutral ramp — aliases onto Bootstrap's theme-aware variables. */
    --app-surface:    var(--bs-body-bg);
    --app-surface-2:  var(--bs-tertiary-bg);
    --app-surface-3:  var(--bs-secondary-bg);
    --app-border:     var(--bs-border-color);
    --app-ink:        var(--bs-body-color);
    --app-ink-strong: var(--bs-emphasis-color);
    --app-ink-dim:    var(--bs-secondary-color);

    /* Accents. Light values first; the dark block re-points them. Each ink is
       chosen to clear 4.5:1 on its own -bg in the theme it belongs to. */
    --app-indigo-bg: #e0e7ff;  --app-indigo-ink: #4338ca;
    --app-green-bg:  #d1fae5;  --app-green-ink:  #047857;
    --app-cyan-bg:   #cffafe;  --app-cyan-ink:   #0e7490;
    --app-amber-bg:  #fef3c7;  --app-amber-ink:  #b45309;
    --app-red-bg:    #fee2e2;  --app-red-ink:    #b91c1c;

    --app-accent:       #4f46e5;   /* focus rings, links, active chips */
    --app-accent-hover: #c7d2fe;   /* hover border */
    --app-accent-fill:  #4338ca;   /* fill under white text */
    /* Primary-action gradient (Add contact / Add member buttons). */
    --app-accent-grad:  linear-gradient(135deg, #4f46e5, #7c3aed);
    --app-on-accent:    #fff;      /* text on that fill — white in both themes */

    --app-th-bg:     #fafbff;      /* table header tint */
    --app-muted-bg:  #f1f5f9;      /* "off" / "none" pills, workspace pill */
    --app-muted-ink: #94a3b8;

    --app-income:  #047857;
    --app-expense: #b91c1c;

    --app-shadow-xs: 0 1px 2px rgba(0,0,0,.04);
    --app-shadow-sm: 0 1px 3px rgba(0,0,0,.06);
    --app-shadow-lg: 0 10px 24px rgba(0,0,0,.07);

    /* Attention surface (terms "important" callout). */
    --app-warn-surface:        #fff8d4;
    --app-warn-surface-border: #f5d671;
    --app-warn-surface-ink:    #6b4d00;

    --app-overlay-soft: rgba(0,0,0,.06);
    --app-avatar-grad:  linear-gradient(135deg, #eef0ff, #fce8ff);

    /* Receivables aging ramp (invoices/reports.php) — five ordered severity
       steps, current → 90+. Kept as its own scale rather than reusing the
       green/amber/red pairs, because the ORDER is the information here and
       three colours can't carry five steps. */
    --app-age-0-bg: #e9f5ec;  --app-age-0-ink: #146c2e;
    --app-age-1-bg: #fff7e0;  --app-age-1-ink: #8a6a00;
    --app-age-2-bg: #ffeacc;  --app-age-2-ink: #8a4b00;
    --app-age-3-bg: #ffd9d9;  --app-age-3-ink: #9b1c1c;
    --app-age-4-bg: #ffc1c1;  --app-age-4-ink: #6b0000;
}

[data-bs-theme="dark"] {
    /* A shade below --bs-body-bg (#212529) so cards and the sidebar lift off
       the canvas instead of melting into it. */
    --app-page-bg:  #16191c;
    --app-page-ink: var(--bs-body-color);

    --app-indigo-bg: rgba(99,102,241,.18);  --app-indigo-ink: #a5b4fc;
    --app-green-bg:  rgba(5,150,105,.18);   --app-green-ink:  #6ee7b7;
    --app-cyan-bg:   rgba(14,165,233,.18);  --app-cyan-ink:   #7dd3fc;
    --app-amber-bg:  rgba(217,119,6,.20);   --app-amber-ink:  #fcd34d;
    --app-red-bg:    rgba(220,38,38,.18);   --app-red-ink:    #fca5a5;

    --app-accent:       #a5b4fc;
    --app-accent-hover: #4f46e5;
    --app-accent-fill:  #4f46e5;   /* stays deep: white text sits on it */
    /* Gradient deepens rather than brightening — white text rides on it. */
    --app-accent-grad:  linear-gradient(135deg, #4338ca, #6d28d9);

    --app-th-bg:     rgba(255,255,255,.04);
    --app-muted-bg:  rgba(255,255,255,.07);
    /* #9aa4b5 measured 4.28:1 once the translucent pill composited over a card
       surface — lightened to clear 4.5:1 there as well as on the page. */
    --app-muted-ink: #a7b0bf;

    --app-income:  #6ee7b7;
    --app-expense: #fca5a5;

    /* Dark surfaces don't take tinted shadows — go deeper, not softer. */
    --app-shadow-xs: 0 1px 2px rgba(0,0,0,.4);
    --app-shadow-sm: 0 1px 3px rgba(0,0,0,.45);
    --app-shadow-lg: 0 10px 24px rgba(0,0,0,.5);

    --app-warn-surface:        #2d2410;
    --app-warn-surface-border: #52421a;
    --app-warn-surface-ink:    #fde68a;

    --app-overlay-soft: rgba(255,255,255,.07);
    --app-avatar-grad:  linear-gradient(135deg, #312e81, #4a1d6e);

    /* Same five-step ramp, rebuilt dark: fills stay translucent washes so the
       progression still reads, inks move to the 300-level shades. */
    --app-age-0-bg: rgba(20,108,46,.22);   --app-age-0-ink: #6ee7b7;
    --app-age-1-bg: rgba(138,106,0,.26);   --app-age-1-ink: #fcd34d;
    --app-age-2-bg: rgba(138,75,0,.30);    --app-age-2-ink: #fdba74;
    --app-age-3-bg: rgba(155,28,28,.28);   --app-age-3-ink: #fca5a5;
    --app-age-4-bg: rgba(185,28,28,.42);   --app-age-4-ink: #fecaca;
}

/* ---------------------------------------------------------------------------
   Bootstrap utilities that are palette colours, not semantic ones, so 5.3
   leaves them light in dark mode. .bg-light is load-bearing: scripts/layout.php
   puts it on <body> and its own sticky-footer rules select `body.bg-light`, so
   the class has to stay — only its colour changes here.
   All four carry !important in Bootstrap, hence the !important.
   --------------------------------------------------------------------------- */
[data-bs-theme="dark"] body.bg-light,
[data-bs-theme="dark"] .bg-light {
    background-color: var(--bs-body-bg) !important;
    color: var(--bs-body-color);
}
[data-bs-theme="dark"] .bg-white {
    background-color: var(--bs-secondary-bg) !important;
}
/* `.card-header.bg-white` is the common "flush header" idiom (three of them on
   invoices/dashboard.php). Needs the extra specificity to beat .card-header's
   own background, and must match the card body rather than .bg-white's
   secondary surface, or the header reads as a separate stripe. */
[data-bs-theme="dark"] .card > .card-header.bg-white,
[data-bs-theme="dark"] .card > .card-footer.bg-white {
    background-color: transparent !important;
}
/* Table variants are fixed palette colours, so a .table-light thead stays
   near-white inside a dark table. Re-point the variant variables Bootstrap
   actually reads rather than setting background-color, so striping, hover and
   borders all stay consistent. */
[data-bs-theme="dark"] .table-light {
    --bs-table-bg: var(--bs-tertiary-bg);
    --bs-table-color: var(--bs-body-color);
    --bs-table-border-color: var(--bs-border-color);
    --bs-table-striped-bg: var(--bs-secondary-bg);
    --bs-table-striped-color: var(--bs-body-color);
    --bs-table-hover-bg: var(--bs-secondary-bg);
    --bs-table-hover-color: var(--bs-body-color);
}
[data-bs-theme="dark"] .text-dark {
    color: var(--bs-emphasis-color) !important;
}
/* .border-light would draw a near-white hairline on a dark panel. */
[data-bs-theme="dark"] .border-light {
    border-color: var(--bs-border-color) !important;
}
/* ...and .border-dark the mirror image: admin/dashboard.php uses
   `border-start border-dark border-5` as a card accent stripe, which on a dark
   panel is a near-black bar on near-black — the accent simply disappears. Map it
   to the accent colour so the stripe still reads as a stripe. */
[data-bs-theme="dark"] .border-dark {
    border-color: var(--app-accent) !important;
}

/* .btn-outline-dark is the same trap as a BUTTON: Bootstrap pins it to a
   near-black ink and border, which on a dark surface is invisible — the
   control is still there and still clickable, it just cannot be seen. It is
   used for the big "Manage All Users" / "Manage Roles" / "Manage Workspaces"
   tiles on admin/dashboard.php and for "Login as" in admin/users-manage.php.
   Bootstrap 5.3 drives buttons entirely off these custom properties, so
   remapping them is enough — no rule needs !important and the light theme is
   untouched. Hover inverts to body-bg on body-colour, matching how
   .btn-outline-dark behaves in light mode. */
[data-bs-theme="dark"] .btn-outline-dark {
    --bs-btn-color:                 var(--bs-body-color);
    --bs-btn-border-color:          var(--bs-border-color);
    --bs-btn-hover-color:           var(--bs-body-bg);
    --bs-btn-hover-bg:              var(--bs-body-color);
    --bs-btn-hover-border-color:    var(--bs-body-color);
    --bs-btn-focus-shadow-rgb:      var(--bs-secondary-rgb);
    --bs-btn-active-color:          var(--bs-body-bg);
    --bs-btn-active-bg:             var(--bs-body-color);
    --bs-btn-active-border-color:   var(--bs-body-color);
    --bs-btn-disabled-color:        var(--bs-secondary-color);
    --bs-btn-disabled-border-color: var(--bs-border-color);
}

/* Escape hatch for a flat dark-ink mark that would vanish on a dark header.
   Opt in per element — never matched automatically, because inverting a colour
   logo wrecks it. */
[data-bs-theme="dark"] .invert-on-dark {
    filter: brightness(0) invert(1) opacity(.9);
}

/* ---------------------------------------------------------------- SweetAlert2
   SweetAlert2 renders into its own container appended to <body> and knows
   nothing about data-bs-theme, so every dialog stayed white in dark mode.

   That was not merely ugly. These dialogs are built from Bootstrap markup —
   accounts/team.php's member editor injects .form-control and .form-select into
   the popup — and in dark mode Bootstrap gives those a dark background with
   light text. Light text in a dark input, sitting on a white popup: the field
   content was effectively invisible while being perfectly focusable.

   Styling the popup itself is enough; the Bootstrap controls inside are already
   correct for dark once they are on a dark surface. Fourteen pages use
   SweetAlert (team, profile, company, contacts, the whole invoices module,
   mykhata and infra layouts), which is why this lives here and not per page.

   Confirm/cancel buttons are left alone — SweetAlert colours them itself and
   they read fine on either surface. */
[data-bs-theme="dark"] .swal2-popup {
    background: var(--bs-body-bg);
    color: var(--bs-body-color);
}
[data-bs-theme="dark"] .swal2-title,
[data-bs-theme="dark"] .swal2-html-container,
[data-bs-theme="dark"] .swal2-label {
    color: var(--bs-body-color);
}
/* SweetAlert's OWN inputs (Swal.fire({input:'text'})) — unlike the Bootstrap
   ones above, these are styled by SweetAlert and stay light without this. */
[data-bs-theme="dark"] .swal2-input,
[data-bs-theme="dark"] .swal2-textarea,
[data-bs-theme="dark"] .swal2-select,
[data-bs-theme="dark"] .swal2-file {
    background: var(--bs-body-bg);
    color: var(--bs-body-color);
    border-color: var(--bs-border-color);
}
[data-bs-theme="dark"] .swal2-input::placeholder,
[data-bs-theme="dark"] .swal2-textarea::placeholder {
    color: var(--bs-secondary-color);
}
[data-bs-theme="dark"] .swal2-validation-message {
    background: var(--app-red-bg);
    color: var(--app-red-ink);
}
[data-bs-theme="dark"] .swal2-footer {
    border-top-color: var(--bs-border-color);
    color: var(--bs-secondary-color);
}
[data-bs-theme="dark"] .swal2-close { color: var(--bs-secondary-color); }
[data-bs-theme="dark"] .swal2-close:hover { color: var(--bs-body-color); }
/* Toasts share .swal2-popup, but carry their own shadow that disappears on a
   dark canvas — give them a border so they stay distinguishable. */
[data-bs-theme="dark"] .swal2-toast {
    border: 1px solid var(--bs-border-color);
}

/* ------------------------------------------------------------------- Select2
   Select2 4.x ships its own fixed light palette (white fields, #444 text,
   #aaa borders) and, like SweetAlert, renders its dropdown into a container
   appended to <body> with no idea data-bs-theme exists. On the dark theme the
   customer picker on invoices/invoices.php?page=invoice_new was a white box
   with dark text sitting in a dark form, and its dropdown a white panel.

   Specificity is deliberate, not accidental: the page loads select2.min.css
   AFTER this file (scripts/layout.php puts app-shell.css in <head>; the page
   adds select2 further down), so a rule of equal weight would lose. Prefixing
   with [data-bs-theme="dark"] adds one attribute selector and wins without
   needing !important anywhere.

   Used by invoices/invoices.php and invoices/payments.php. */
[data-bs-theme="dark"] .select2-container--default .select2-selection--single,
[data-bs-theme="dark"] .select2-container--default .select2-selection--multiple {
    background-color: var(--bs-body-bg);
    border-color: var(--bs-border-color);
}
[data-bs-theme="dark"] .select2-container--default .select2-selection--single .select2-selection__rendered {
    color: var(--bs-body-color);
}
[data-bs-theme="dark"] .select2-container--default .select2-selection--single .select2-selection__placeholder {
    color: var(--bs-secondary-color);
}
/* The caret is drawn as a CSS triangle via border-color, so it needs the ink
   colour rather than a background. */
[data-bs-theme="dark"] .select2-container--default .select2-selection--single .select2-selection__arrow b {
    border-color: var(--bs-secondary-color) transparent transparent transparent;
}
[data-bs-theme="dark"] .select2-container--default.select2-container--open .select2-selection--single .select2-selection__arrow b {
    border-color: transparent transparent var(--bs-secondary-color) transparent;
}
/* The dropdown panel itself — appended to <body>, hence no ancestor scoping. */
[data-bs-theme="dark"] .select2-dropdown {
    background-color: var(--bs-body-bg);
    border-color: var(--bs-border-color);
    color: var(--bs-body-color);
}
[data-bs-theme="dark"] .select2-container--default .select2-search--dropdown .select2-search__field {
    background-color: var(--bs-body-bg);
    color: var(--bs-body-color);
    border-color: var(--bs-border-color);
}
[data-bs-theme="dark"] .select2-container--default .select2-results__option {
    color: var(--bs-body-color);
}
[data-bs-theme="dark"] .select2-container--default .select2-results__option--highlighted[aria-selected],
[data-bs-theme="dark"] .select2-container--default .select2-results__option--highlighted {
    background-color: var(--app-accent-fill);
    color: var(--app-on-accent);
}
[data-bs-theme="dark"] .select2-container--default .select2-results__option[aria-selected="true"] {
    background-color: var(--app-muted-bg);
    color: var(--bs-body-color);
}
[data-bs-theme="dark"] .select2-container--default .select2-results__message {
    color: var(--bs-secondary-color);
}
/* Multi-select chips, for any page that switches to one later. */
[data-bs-theme="dark"] .select2-container--default .select2-selection--multiple .select2-selection__choice {
    background-color: var(--app-muted-bg);
    border-color: var(--bs-border-color);
    color: var(--bs-body-color);
}
[data-bs-theme="dark"] .select2-container--default.select2-container--disabled .select2-selection--single {
    background-color: var(--bs-secondary-bg);
}
