/* ==========================================================================
   Audit Hive – Theme Token Layer
   ---------------------------------------------------------------------------
   ONE source of truth for colours, surfaces, radii, shadows and motion.
   Light values live on :root, dark values on html[data-bs-theme="dark"].
   Bootstrap's --bs-* variables are mapped to these tokens so the whole
   template (and every page CSS that uses --bs-*) follows automatically.

   Rules:
   - Never hard-code a colour in page CSS – use a --ah-* or --bs-* token.
   - Depth in LIGHT mode = shadows.  Depth in DARK mode = edge highlight + glow.
   ========================================================================== */

:root {
    /* ---- Brand ---------------------------------------------------------- */
    --ah-primary:          #3b5bfd;
    --ah-primary-rgb:      59, 91, 253;
    --ah-primary-hover:    #2947e3;
    --ah-primary-soft:     rgba(59, 91, 253, 0.10);
    --ah-primary-gradient: linear-gradient(135deg, #4f6dff 0%, #3b5bfd 55%, #2e46d6 100%);

    --ah-accent:           #06b6d4;
    --ah-accent-rgb:       6, 182, 212;
    --ah-success:          #10b981;  --ah-success-rgb: 16, 185, 129;
    --ah-warning:          #f59e0b;  --ah-warning-rgb: 245, 158, 11;
    --ah-danger:           #ef4444;  --ah-danger-rgb:  239, 68, 68;
    --ah-info:             #0ea5e9;  --ah-info-rgb:    14, 165, 233;
    --ah-purple:           #8b5cf6;  --ah-purple-rgb:  139, 92, 246;
    --ah-pink:             #ec4899;  --ah-pink-rgb:    236, 72, 153;
    --ah-critical:         #dc2626;

    /* ---- Surfaces --------------------------------------------------------- */
    --ah-bg:               #eef1f6;
    --ah-bg-rgb:           238, 241, 246;
    --ah-surface:          #ffffff;          /* cards, tables, modals          */
    --ah-surface-rgb:      255, 255, 255;
    --ah-surface-2:        #f8fafc;          /* card headers, table head, wells */
    --ah-surface-3:        #eef2f7;          /* hover rows, inputs              */
    --ah-elevated:         #ffffff;          /* dropdowns, popovers             */

    /* ---- Text ------------------------------------------------------------- */
    --ah-heading:          #0f172a;
    --ah-text:             #1e293b;
    --ah-text-2:           #64748b;
    --ah-text-3:           #94a3b8;
    --ah-text-on-primary:  #ffffff;

    /* ---- Lines ------------------------------------------------------------ */
    --ah-border:           rgba(15, 23, 42, 0.08);
    --ah-border-strong:    rgba(15, 23, 42, 0.14);
    --ah-divider:          rgba(15, 23, 42, 0.06);

    /* ---- Chrome (sidebar / topbar) ---------------------------------------- */
    --ah-sidebar-bg:       linear-gradient(180deg, #101d3a 0%, #0a1832 60%, #081328 100%);
    --ah-sidebar-text:     #b6c0d8;
    --ah-sidebar-text-active: #ffffff;
    --ah-sidebar-active-bg: rgba(255, 255, 255, 0.08);
    --ah-sidebar-glow:     rgba(99, 125, 255, 0.45);
    --ah-topbar-bg:        rgba(255, 255, 255, 0.78);
    --ah-topbar-border:    rgba(15, 23, 42, 0.06);

    /* ---- Radius scale ----------------------------------------------------- */
    --ah-radius-xs:  6px;
    --ah-radius-sm:  8px;
    --ah-radius-md:  12px;
    --ah-radius-lg:  16px;
    --ah-radius-xl:  24px;
    --ah-radius-pill: 999px;

    /* ---- Depth (light = shadows) ------------------------------------------ */
    --ah-shadow-xs:  0 1px 2px rgba(15, 23, 42, 0.05);
    --ah-shadow-sm:  0 1px 2px rgba(15, 23, 42, 0.05), 0 2px 8px rgba(15, 23, 42, 0.05);
    --ah-shadow-md:  0 2px 4px rgba(15, 23, 42, 0.04), 0 10px 24px -6px rgba(15, 23, 42, 0.12);
    --ah-shadow-lg:  0 4px 8px rgba(15, 23, 42, 0.05), 0 24px 48px -12px rgba(15, 23, 42, 0.18);
    --ah-shadow-glow: 0 0 0 1px rgba(var(--ah-primary-rgb), 0.18), 0 8px 24px -4px rgba(var(--ah-primary-rgb), 0.35);
    --ah-highlight:  inset 0 1px 0 rgba(255, 255, 255, 0.85);   /* top edge light */
    --ah-glass-bg:   rgba(255, 255, 255, 0.62);
    --ah-glass-border: rgba(255, 255, 255, 0.55);

    /* ---- Misc ------------------------------------------------------------- */
    --ah-preloader-bg: rgba(238, 241, 246, 0.90);
    --ah-font:         "Nunito", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --ah-ease:         cubic-bezier(0.22, 1, 0.36, 1);
    --ah-dur-fast:     150ms;
    --ah-dur:          250ms;
    --ah-dur-slow:     400ms;
    --ah-focus-ring:   0 0 0 3px rgba(var(--ah-primary-rgb), 0.28);

    /* ---- Chart palette (read by JS via getComputedStyle) ------------------ */
    --ah-chart-1: #3b5bfd;
    --ah-chart-2: #10b981;
    --ah-chart-3: #8b5cf6;
    --ah-chart-4: #f59e0b;
    --ah-chart-5: #06b6d4;
    --ah-chart-6: #ec4899;
    --ah-chart-grid: rgba(15, 23, 42, 0.07);
    --ah-chart-text: #64748b;

    /* ======================================================================
       Bootstrap mapping – keeps the template & every page CSS in sync
       ====================================================================== */
    --bs-body-bg:            var(--ah-bg);
    --bs-body-bg-rgb:        var(--ah-bg-rgb);
    --bs-body-color:         var(--ah-text);
    --bs-emphasis-color:     var(--ah-heading);
    --bs-heading-color:      var(--ah-heading);
    --bs-secondary-color:    var(--ah-text-2);
    --bs-tertiary-color:     var(--ah-text-3);
    --bs-card-bg:            var(--ah-surface);
    --bs-tertiary-bg:        var(--ah-surface-2);
    --bs-secondary-bg:       var(--ah-surface-3);
    --bs-border-color:       var(--ah-border);
    --bs-border-color-translucent: var(--ah-border);
    --bs-primary:            var(--ah-primary);
    --bs-primary-rgb:        var(--ah-primary-rgb);
    --bs-success:            var(--ah-success);  --bs-success-rgb: var(--ah-success-rgb);
    --bs-warning:            var(--ah-warning);  --bs-warning-rgb: var(--ah-warning-rgb);
    --bs-danger:             var(--ah-danger);   --bs-danger-rgb:  var(--ah-danger-rgb);
    --bs-info:               var(--ah-info);     --bs-info-rgb:    var(--ah-info-rgb);
    --bs-purple:             var(--ah-purple);   --bs-purple-rgb:  var(--ah-purple-rgb);
    --bs-pink:               var(--ah-pink);     --bs-pink-rgb:    var(--ah-pink-rgb);
    --bs-link-color:         var(--ah-primary);
    --bs-link-hover-color:   var(--ah-primary-hover);
    --bs-modal-bg:           var(--ah-surface);
    --bs-table-hover-bg:     var(--ah-surface-3);
    --bs-font-sans-serif:    var(--ah-font);

    /* Legacy aliases still referenced by older page CSS */
    --ah-secondary:          var(--ah-text-2);
    --ah-primary-light:      var(--ah-primary-soft);
    --ah-card-shadow:        var(--ah-shadow-sm);
    --ah-card-shadow-hover:  var(--ah-shadow-md);
    --dash-primary:          var(--ah-primary);
    --dash-success:          var(--ah-success);
    --dash-info:             var(--ah-info);
    --dash-warning:          var(--ah-warning);
    --dash-danger:           var(--ah-danger);
    --dash-purple:           var(--ah-purple);
    --dash-secondary:        var(--ah-text-2);
}

/* ==========================================================================
   DARK THEME
   ========================================================================== */
html[data-bs-theme="dark"] {
    --ah-primary:          #6b85ff;           /* lifted for contrast on dark  */
    --ah-primary-rgb:      107, 133, 255;
    --ah-primary-hover:    #8599ff;
    --ah-primary-soft:     rgba(107, 133, 255, 0.14);
    --ah-primary-gradient: linear-gradient(135deg, #7b93ff 0%, #5d79ff 55%, #4a63e6 100%);

    --ah-success: #34d399;  --ah-success-rgb: 52, 211, 153;
    --ah-warning: #fbbf24;  --ah-warning-rgb: 251, 191, 36;
    --ah-danger:  #f87171;  --ah-danger-rgb:  248, 113, 113;
    --ah-info:    #38bdf8;  --ah-info-rgb:    56, 189, 248;
    --ah-purple:  #a78bfa;  --ah-purple-rgb:  167, 139, 250;
    --ah-pink:    #f472b6;  --ah-pink-rgb:    244, 114, 182;

    --ah-bg:          #0f1420;
    --ah-bg-rgb:      15, 20, 32;
    --ah-surface:     #171d2c;
    --ah-surface-rgb: 23, 29, 44;
    --ah-surface-2:   #1c2334;
    --ah-surface-3:   #222a3d;
    --ah-elevated:    #222a3d;

    --ah-heading:     #f1f5f9;
    --ah-text:        #e2e8f0;
    --ah-text-2:      #9aa5bd;
    --ah-text-3:      #6b7690;

    --ah-border:        rgba(255, 255, 255, 0.08);
    --ah-border-strong: rgba(255, 255, 255, 0.14);
    --ah-divider:       rgba(255, 255, 255, 0.06);

    --ah-sidebar-bg:    linear-gradient(180deg, #0c1322 0%, #0a1020 60%, #080d1a 100%);
    --ah-sidebar-text:  #aab4cc;
    --ah-topbar-bg:     rgba(23, 29, 44, 0.78);
    --ah-topbar-border: rgba(255, 255, 255, 0.06);

    /* Depth (dark = edge light + glow, soft black ambient) */
    --ah-shadow-xs:  0 1px 2px rgba(0, 0, 0, 0.3);
    --ah-shadow-sm:  0 1px 2px rgba(0, 0, 0, 0.35), 0 2px 10px rgba(0, 0, 0, 0.30);
    --ah-shadow-md:  0 2px 4px rgba(0, 0, 0, 0.35), 0 12px 28px -8px rgba(0, 0, 0, 0.55);
    --ah-shadow-lg:  0 4px 8px rgba(0, 0, 0, 0.4),  0 28px 56px -14px rgba(0, 0, 0, 0.7);
    --ah-shadow-glow: 0 0 0 1px rgba(var(--ah-primary-rgb), 0.35), 0 8px 28px -4px rgba(var(--ah-primary-rgb), 0.45);
    --ah-highlight:  inset 0 1px 0 rgba(255, 255, 255, 0.07);
    --ah-glass-bg:   rgba(23, 29, 44, 0.62);
    --ah-glass-border: rgba(255, 255, 255, 0.10);

    --ah-preloader-bg: rgba(15, 20, 32, 0.92);
    --ah-focus-ring:   0 0 0 3px rgba(var(--ah-primary-rgb), 0.38);

    --ah-chart-1: #6b85ff;
    --ah-chart-2: #34d399;
    --ah-chart-3: #a78bfa;
    --ah-chart-4: #fbbf24;
    --ah-chart-5: #38bdf8;
    --ah-chart-6: #f472b6;
    --ah-chart-grid: rgba(255, 255, 255, 0.08);
    --ah-chart-text: #9aa5bd;

    /* Bootstrap dark mapping (formerly injected by layout.js) */
    --bs-body-bg:            var(--ah-bg);
    --bs-body-bg-rgb:        var(--ah-bg-rgb);
    --bs-body-color:         var(--ah-text);
    --bs-body-color-rgb:     226, 232, 240;
    --bs-emphasis-color:     var(--ah-heading);
    --bs-heading-color:      var(--ah-heading);
    --bs-secondary-color:    var(--ah-text-2);
    --bs-tertiary-color:     var(--ah-text-3);
    --bs-card-bg:            var(--ah-surface);
    --bs-tertiary-bg:        var(--ah-surface-2);
    --bs-secondary-bg:       var(--ah-surface-3);
    --bs-border-color:       var(--ah-border);
    --bs-border-color-translucent: var(--ah-border);
    --bs-light:              var(--ah-surface-3);
    --bs-light-rgb:          34, 42, 61;
    --bs-dark:               #0b0f19;
    --bs-dark-rgb:           11, 15, 25;
    --bs-gray-100:           #1c2334;
    --bs-gray-200:           #222a3d;
    --bs-gray-300:           #2b3447;
    --bs-primary:            var(--ah-primary);
    --bs-primary-rgb:        var(--ah-primary-rgb);
    --bs-success:            var(--ah-success);  --bs-success-rgb: var(--ah-success-rgb);
    --bs-warning:            var(--ah-warning);  --bs-warning-rgb: var(--ah-warning-rgb);
    --bs-danger:             var(--ah-danger);   --bs-danger-rgb:  var(--ah-danger-rgb);
    --bs-info:               var(--ah-info);     --bs-info-rgb:    var(--ah-info-rgb);
    --bs-purple:             var(--ah-purple);   --bs-purple-rgb:  var(--ah-purple-rgb);
    --bs-pink:               var(--ah-pink);     --bs-pink-rgb:    var(--ah-pink-rgb);
    --bs-link-color:         var(--ah-primary);
    --bs-link-hover-color:   var(--ah-primary-hover);
    --bs-modal-bg:           var(--ah-surface);
    --bs-modal-color:        var(--ah-text);
    --bs-table-hover-bg:     var(--ah-surface-3);
    color-scheme: dark;
}
:root { color-scheme: light; }

/* ==========================================================================
   Base
   ========================================================================== */
body {
    background-color: var(--bs-body-bg);
    color: var(--bs-body-color);
    font-family: var(--ah-font);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background-color var(--ah-dur) ease, color var(--ah-dur) ease;
}
h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 { color: var(--ah-heading); }

/* ==========================================================================
   Preloader – dark-safe, short fade
   ========================================================================== */
#preloader {
    position: fixed; inset: 0;
    width: 100%; height: 100%;
    background: var(--ah-preloader-bg);
    -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
    z-index: 9999;
    display: none;
    transition: opacity 180ms ease;
}
html.loading #preloader { display: flex !important; align-items: center; justify-content: center; }
html.loading #preloader #status { display: block !important; }   /* app.js fades #status out once; make sure it comes back */
#preloader.is-leaving { opacity: 0; pointer-events: none; }
#preloader #status { text-align: center; color: var(--ah-text-2); }
#preloader .spinner-border { color: var(--ah-primary) !important; border-width: 3px; }
/* login-layout spinner (template .spinner) */
#preloader .spinner { border-top-color: var(--ah-primary); }

/* ==========================================================================
   Theme toggle (topbar) & Appearance control (settings drawer)
   ========================================================================== */
.ah-theme-toggle { position: relative; }
.ah-theme-toggle .ah-icon-light,
.ah-theme-toggle .ah-icon-dark { transition: transform var(--ah-dur) var(--ah-ease), opacity var(--ah-dur) ease; }
.ah-theme-toggle .ah-icon-dark  { display: inline-block; }
.ah-theme-toggle .ah-icon-light { display: none; }
html[data-bs-theme="dark"] .ah-theme-toggle .ah-icon-dark  { display: none; }
html[data-bs-theme="dark"] .ah-theme-toggle .ah-icon-light { display: inline-block; }
.ah-theme-toggle:hover i { transform: rotate(20deg) scale(1.1); }
.ah-theme-toggle--floating {
    position: fixed; top: 16px; right: 16px; z-index: 1050;
    width: 42px; height: 42px; border-radius: var(--ah-radius-pill);
    border: 1px solid var(--ah-border); background: var(--ah-glass-bg);
    -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
    color: var(--ah-text-2); font-size: 1.2rem; display: inline-flex; align-items: center; justify-content: center;
    box-shadow: var(--ah-shadow-sm), var(--ah-highlight); cursor: pointer;
    transition: transform var(--ah-dur-fast) var(--ah-ease), box-shadow var(--ah-dur-fast) ease, color var(--ah-dur-fast) ease;
}
.ah-theme-toggle--floating:hover { color: var(--ah-primary); transform: translateY(-1px); box-shadow: var(--ah-shadow-md), var(--ah-highlight); }

.ah-appearance { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; padding: 4px; border-radius: var(--ah-radius-md); background: var(--ah-surface-3); border: 1px solid var(--ah-border); }
.ah-appearance input { position: absolute; opacity: 0; pointer-events: none; }
.ah-appearance label {
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    padding: 10px 6px; border-radius: var(--ah-radius-sm); cursor: pointer;
    font-size: 0.78rem; font-weight: 600; color: var(--ah-text-2);
    transition: background var(--ah-dur-fast) ease, color var(--ah-dur-fast) ease, box-shadow var(--ah-dur-fast) ease;
}
.ah-appearance label i { font-size: 1.25rem; }
.ah-appearance input:checked + label { background: var(--ah-surface); color: var(--ah-primary); box-shadow: var(--ah-shadow-sm), var(--ah-highlight); }
.ah-appearance input:focus-visible + label { box-shadow: var(--ah-focus-ring); }
.right-bar { background: var(--ah-surface); color: var(--ah-text); box-shadow: var(--ah-shadow-lg); }
.right-bar .rightbar-title { background: var(--ah-surface-2); border-bottom: 1px solid var(--ah-border); }
.right-bar hr { border-color: var(--ah-border); opacity: 1; }

/* ==========================================================================
   Accessibility / motion
   ========================================================================== */
:focus-visible { outline: none; box-shadow: var(--ah-focus-ring); }
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
