/* ================================================================
   CML Dashboard — Polish Layer
   ----------------------------------------------------------------
   Loaded AFTER /mobile.css. Adds restrained glass-morphism,
   material depth, and reveal animations. UI polish only —
   no layout, no business logic, no data shape changes.

   Cascade order:
     1. inline page <style>     — base layout + tokens
     2. /mobile.css             — phone + tablet overrides
     3. /polish.css (this file) — universal aesthetic polish

   Class prefixes:
     .pr-*    polish-specific utilities
     .js-*    state classes added by polish.js on load
   ================================================================ */

/* ----------------------------------------------------------------
   1. POLISH TOKENS (additive — do not override existing)
   ---------------------------------------------------------------- */
:root {
  /* Glass surface colours */
  --pr-surface:        rgba(26, 30, 39, 0.68);     /* card / panel default */
  --pr-surface-hover:  rgba(28, 33, 43, 0.82);     /* card / panel hover   */
  --pr-surface-strong: rgba(19, 22, 29, 0.92);     /* modal, mobile drawer */
  --pr-surface-header: rgba(19, 22, 29, 0.78);     /* sticky header / nav  */

  /* Borders and highlights */
  --pr-border:         rgba(255, 255, 255, 0.07);
  --pr-border-bright:  rgba(255, 255, 255, 0.13);
  --pr-top-highlight:  rgba(255, 255, 255, 0.05);  /* "brighter top edge"  */
  --pr-inner-glow:     rgba(255, 255, 255, 0.03);

  /* Shadows — soft depth, not heavy drops */
  --pr-shadow-soft:    0 1px 2px rgba(0,0,0,0.25), 0 10px 28px rgba(0,0,0,0.20);
  --pr-shadow-lift:    0 2px 5px rgba(0,0,0,0.30), 0 18px 48px rgba(0,0,0,0.30);
  --pr-shadow-modal:   0 4px 12px rgba(0,0,0,0.35), 0 32px 80px rgba(0,0,0,0.45);

  /* Blur radii — modest by default, reduced on mobile */
  --pr-blur:           10px;
  --pr-blur-modal:     16px;
  --pr-blur-overlay:   8px;

  /* Motion timing — match user-stated budget */
  --pr-t-fast:   120ms cubic-bezier(.4, 0, .2, 1);
  --pr-t-base:   180ms cubic-bezier(.4, 0, .2, 1);
  --pr-t-reveal: 240ms cubic-bezier(.4, 0, .2, 1);
  --pr-t-modal:  220ms cubic-bezier(.16, 1, .3, 1);
}

.cml-status-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 18px;
  border-bottom: 1px solid var(--pr-border);
  background: rgba(19, 22, 29, 0.72);
  -webkit-backdrop-filter: blur(10px) saturate(130%);
  backdrop-filter: blur(10px) saturate(130%);
  color: var(--text-3);
  font-size: 11px;
  line-height: 1.35;
}

.cml-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: var(--green);
  box-shadow: 0 0 0 3px rgba(39, 194, 140, 0.10);
  flex: 0 0 auto;
}

.cml-status-strip.is-offline .cml-status-dot {
  background: var(--red);
  box-shadow: 0 0 0 3px rgba(229, 72, 77, 0.12);
}

.cml-status-copy {
  min-width: 0;
  flex: 1;
}

.cml-status-mode {
  flex: 0 0 auto;
  color: var(--orange);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* @supports fallback — backdrop-filter unsupported (older browsers) */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  :root {
    --pr-surface:        rgba(26, 30, 39, 0.96);
    --pr-surface-hover:  rgba(28, 33, 43, 1);
    --pr-surface-strong: rgba(19, 22, 29, 1);
    --pr-surface-header: rgba(19, 22, 29, 0.98);
  }
}

/* ----------------------------------------------------------------
   2. KPI CARDS — primary glass surface
   ---------------------------------------------------------------- */
.kpi-card {
  background: var(--pr-surface);
  -webkit-backdrop-filter: blur(var(--pr-blur)) saturate(135%);
  backdrop-filter: blur(var(--pr-blur)) saturate(135%);
  border: 1px solid var(--pr-border);
  box-shadow:
    inset 0 1px 0 var(--pr-top-highlight),       /* slightly brighter top edge */
    inset 0 0 0 1px var(--pr-inner-glow),         /* subtle inner glow */
    var(--pr-shadow-soft);
  transition:
    background-color var(--pr-t-base),
    border-color     var(--pr-t-base),
    box-shadow       var(--pr-t-base),
    transform        var(--pr-t-fast);
}

.kpi-card:hover {
  background: var(--pr-surface-hover);
  border-color: var(--pr-border-bright);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.07),
    var(--pr-shadow-lift);
  transform: translateY(-1px);
}

.kpi-card:active {
  transform: translateY(0);
  transition-duration: 80ms;
}

/* ----------------------------------------------------------------
   3. PANELS — glass surface, slightly more solid than cards
   ---------------------------------------------------------------- */
.panel {
  background: var(--pr-surface);
  -webkit-backdrop-filter: blur(var(--pr-blur)) saturate(130%);
  backdrop-filter: blur(var(--pr-blur)) saturate(130%);
  border: 1px solid var(--pr-border);
  box-shadow:
    inset 0 1px 0 var(--pr-top-highlight),
    var(--pr-shadow-soft);
  transition:
    border-color var(--pr-t-base),
    box-shadow   var(--pr-t-base);
}

.panel:hover {
  border-color: var(--pr-border-bright);
}

/* Panel headers — soft gradient suggesting light source from above */
.panel-head {
  border-bottom: 1px solid var(--pr-border);
  background: linear-gradient(180deg, rgba(255,255,255,0.025), transparent);
}

/* ----------------------------------------------------------------
   4. DRILL MODAL / OVERLAY — dimmed + blurred backing
   ---------------------------------------------------------------- */
.kd-overlay {
  background: rgba(11, 13, 18, 0.55);
  -webkit-backdrop-filter: blur(var(--pr-blur-overlay));
  backdrop-filter: blur(var(--pr-blur-overlay));
  transition: opacity var(--pr-t-modal);
}

.kd-modal {
  background: var(--pr-surface-strong);
  -webkit-backdrop-filter: blur(var(--pr-blur-modal)) saturate(140%);
  backdrop-filter: blur(var(--pr-blur-modal)) saturate(140%);
  border: 1px solid var(--pr-border-bright);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    var(--pr-shadow-modal);
}

.kd-close {
  transition: background-color var(--pr-t-fast), color var(--pr-t-fast);
}

.kd-close:hover {
  background: rgba(255, 255, 255, 0.07);
}

/* ----------------------------------------------------------------
   5. STICKY HEADER (top bar with date-range pills)
   ---------------------------------------------------------------- */
.header {
  background: var(--pr-surface-header);
  -webkit-backdrop-filter: blur(12px) saturate(130%);
  backdrop-filter: blur(12px) saturate(130%);
  border-bottom: 1px solid var(--pr-border);
}

/* ----------------------------------------------------------------
   6. PERIOD PICKER (MTD / YTD / Last Mo …)
   ---------------------------------------------------------------- */
.period-btn {
  transition:
    background-color var(--pr-t-fast),
    border-color     var(--pr-t-fast),
    color            var(--pr-t-fast),
    box-shadow       var(--pr-t-fast);
}

.period-btn:hover {
  border-color: var(--pr-border-bright);
  background: rgba(255, 255, 255, 0.03);
}

.period-btn.active,
.period-btn.is-active,
.period-btn[aria-pressed="true"] {
  background: rgba(242, 106, 46, 0.12);
  border-color: rgba(242, 106, 46, 0.45);
  color: var(--orange);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

/* ----------------------------------------------------------------
   7. SIDEBAR NAV
   ---------------------------------------------------------------- */
.nav-item {
  transition:
    background-color var(--pr-t-fast),
    color            var(--pr-t-fast),
    box-shadow       var(--pr-t-fast);
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.03);
}

.nav-item.active,
.nav-item.is-active,
.nav-item[aria-current="page"] {
  background: rgba(242, 106, 46, 0.10);
  color: var(--text);
  box-shadow:
    inset 2px 0 0 var(--orange),
    inset 0 1px 0 var(--pr-top-highlight);
}

/* ----------------------------------------------------------------
   8. ICON BUTTONS / GENERIC BUTTONS — focus + hover polish
   ---------------------------------------------------------------- */
.icon-btn,
.kpi-pin {
  transition:
    background-color var(--pr-t-fast),
    border-color     var(--pr-t-fast),
    color            var(--pr-t-fast);
}
.icon-btn:hover,
.kpi-pin:hover {
  background: rgba(255, 255, 255, 0.05);
  border-color: var(--pr-border-bright);
}

button:focus-visible,
.nav-item:focus-visible,
.kpi-card:focus-visible,
.period-btn:focus-visible,
.icon-btn:focus-visible {
  outline: 2px solid rgba(91, 168, 245, 0.6);
  outline-offset: 2px;
}

/* ----------------------------------------------------------------
   9. BADGES + TAGS — restrained contrast
   ---------------------------------------------------------------- */
.badge,
.tag {
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

/* ----------------------------------------------------------------
   10. MOBILE-SPECIFIC OVERRIDES (≤640px)
       - Reduce blur for performance on iPhone Safari
       - Make surfaces less translucent for readability
   ---------------------------------------------------------------- */
@media (max-width: 640px) {
  :root {
    --pr-blur:         5px;
    --pr-blur-modal:   8px;
    --pr-blur-overlay: 4px;
    --pr-surface:        rgba(26, 30, 39, 0.88);
    --pr-surface-hover:  rgba(28, 33, 43, 0.95);
    --pr-surface-strong: rgba(19, 22, 29, 0.98);
    --pr-surface-header: rgba(19, 22, 29, 0.92);
  }

  /* Mobile drawer (sidebar slide-out) */
  .sidebar {
    background: var(--pr-surface-strong);
    -webkit-backdrop-filter: blur(16px) saturate(140%);
    backdrop-filter: blur(16px) saturate(140%);
    border-right: 1px solid var(--pr-border);
  }

  /* Disable expensive overlay blur on phone — full-screen modal anyway */
  .kd-overlay {
    background: rgba(11, 13, 18, 0.88);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .kd-modal {
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    background: var(--pr-surface-strong);
  }

  /* Hover transform off on phone (no hover, and removes flicker on tap) */
  .kpi-card:hover {
    transform: none;
  }
}

/* ----------------------------------------------------------------
   11. REVEAL ANIMATIONS
       - Page-shell fade + KPI/panel stagger
       - polish.js adds `.js-polish` to <html>, then `.pr-in` to
         each element when it enters the viewport.
       - Content is VISIBLE BY DEFAULT — if polish.js never runs,
         no class is added and nothing is hidden.
   ---------------------------------------------------------------- */
.js-polish .kpi-card,
.js-polish .panel {
  opacity: 0;
  transform: translateY(6px);
  transition:
    opacity   var(--pr-t-reveal),
    transform var(--pr-t-reveal),
    background-color var(--pr-t-base),
    border-color     var(--pr-t-base),
    box-shadow       var(--pr-t-base);
}

.js-polish .kpi-card.pr-in,
.js-polish .panel.pr-in {
  opacity: 1;
  transform: none;
}

/* KPI grid stagger — first 8 cards only, max ~280ms total */
.js-polish .kpi-row > .kpi-card:nth-child(1) { transition-delay: 0ms; }
.js-polish .kpi-row > .kpi-card:nth-child(2) { transition-delay: 40ms; }
.js-polish .kpi-row > .kpi-card:nth-child(3) { transition-delay: 80ms; }
.js-polish .kpi-row > .kpi-card:nth-child(4) { transition-delay: 120ms; }
.js-polish .kpi-row > .kpi-card:nth-child(5) { transition-delay: 160ms; }
.js-polish .kpi-row > .kpi-card:nth-child(6) { transition-delay: 200ms; }
.js-polish .kpi-row > .kpi-card:nth-child(7) { transition-delay: 240ms; }
.js-polish .kpi-row > .kpi-card:nth-child(n+8) { transition-delay: 280ms; }

/* Page-shell fade — content area only, runs once via .js-polish-loaded */
.js-polish .content {
  animation: prShellFade 160ms cubic-bezier(.4, 0, .2, 1) both;
}
@keyframes prShellFade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ----------------------------------------------------------------
   12. REDUCED MOTION — kill all transitions, animations, stagger
   ---------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
  .js-polish .kpi-card,
  .js-polish .panel,
  .js-polish .kpi-row > .kpi-card {
    opacity: 1 !important;
    transform: none !important;
    transition-delay: 0ms !important;
  }
  .js-polish .content {
    animation: none !important;
  }
}

/* ================================================================
   13. CHART REVEAL — grow on first viewport entry
   ----------------------------------------------------------------
   Bars: clip-path wipes up from baseline, so bars appear to grow
         out of the x-axis.
   Lines: stroke-dashoffset draws the path on. polish.js measures
          path length via getTotalLength() and sets --pl per path.
   Both gated by the existing .pr-in class added on reveal.
   ================================================================ */
.js-polish .panel .bar-chart-area,
.js-polish .panel .bar-chart-outer,
.js-polish .panel .chart-area,
.js-polish .panel .chart-wrap {
  clip-path: inset(100% 0 0 0);
  -webkit-clip-path: inset(100% 0 0 0);
  transition:
    clip-path 900ms cubic-bezier(.16, 1, .3, 1) 120ms,
    -webkit-clip-path 900ms cubic-bezier(.16, 1, .3, 1) 120ms;
}
.js-polish .panel.pr-in .bar-chart-area,
.js-polish .panel.pr-in .bar-chart-outer,
.js-polish .panel.pr-in .chart-area,
.js-polish .panel.pr-in .chart-wrap {
  clip-path: inset(0 0 0 0);
  -webkit-clip-path: inset(0 0 0 0);
}

/* Lines/polylines tagged by polish.js with .pr-draw */
.js-polish .panel .pr-draw {
  stroke-dasharray: var(--pl, 2000);
  stroke-dashoffset: var(--pl, 2000);
  transition: stroke-dashoffset 1200ms cubic-bezier(.16, 1, .3, 1) 320ms;
}
.js-polish .panel.pr-in .pr-draw {
  stroke-dashoffset: 0;
}

/* ================================================================
   14. KPI COUNT-UP — running number while polish.js animates
   ----------------------------------------------------------------
   Numbers tick from 0 to their real value. We add a temporary
   .pr-counting class while the rAF loop runs. Used here only to
   tighten letter-spacing slightly so the variable-width tabular
   feel stays consistent while digits change.
   ================================================================ */
.kpi-value {
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum';
}

.kpi-card.pr-counting .kpi-value {
  letter-spacing: -0.005em;
}

/* ================================================================
   15. SKELETON SHIMMER — KPI values that haven't loaded yet
   ----------------------------------------------------------------
   When .pr-loading is on a card (added by polish.js when the
   value text is "—", empty, or "Loading…"), the kpi-value text
   is hidden and replaced with a shimmering bar.
   ================================================================ */
.kpi-card.pr-loading .kpi-value {
  position: relative;
  color: transparent !important;
  user-select: none;
  min-height: 1.1em;
}
.kpi-card.pr-loading .kpi-value::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
  width: 62%;
  height: 0.58em;
  min-height: 14px;
  border-radius: 4px;
  background: linear-gradient(
    90deg,
    rgba(255,255,255,0.04) 0%,
    rgba(255,255,255,0.11) 50%,
    rgba(255,255,255,0.04) 100%
  );
  background-size: 250% 100%;
  animation: prShimmer 1.6s ease-in-out infinite;
}

@keyframes prShimmer {
  0%   { background-position: 250% 0; }
  100% { background-position: -150% 0; }
}

/* ================================================================
   16. AMBIENT LIGHT SHIFT — fixed background layer behind content
   ----------------------------------------------------------------
   polish.js injects a fixed-position .pr-ambient layer into <body>
   (only if reduced-motion is OFF). Subtle warm + cool wash that
   drifts ~2% over 32s. Sits behind all content via z-index: -1.
   ================================================================ */
.pr-ambient {
  position: fixed;
  inset: -4vh -4vw;
  pointer-events: none;
  /* z-index: -1 keeps the ambient layer behind everything without needing
     .app to have z-index: 1 — which previously created a stacking context
     that trapped the mobile sidebar (z:1000) below the body-level
     backdrop (z:999), blocking clicks on nav items. */
  z-index: -1;
  background:
    radial-gradient(circle 460px at 18% 24%, rgba(242, 106, 46, 0.045), transparent 65%),
    radial-gradient(circle 400px at 84% 76%, rgba(91, 168, 245, 0.040), transparent 65%);
  opacity: 0;
  animation:
    prAmbientFade 1.4s ease 240ms forwards,
    prAmbientShift 32s ease-in-out 1.8s infinite alternate;
  will-change: transform;
}

@keyframes prAmbientFade {
  to { opacity: 1; }
}
@keyframes prAmbientShift {
  to { transform: translate3d(2%, -1.2%, 0); }
}

/* ================================================================
   17. EXTENDED REDUCED-MOTION OVERRIDES (for sections 13–16)
   ================================================================ */
@media (prefers-reduced-motion: reduce) {
  .js-polish .panel .bar-chart-area,
  .js-polish .panel .bar-chart-outer,
  .js-polish .panel .chart-area,
  .js-polish .panel .chart-wrap {
    clip-path: none !important;
    -webkit-clip-path: none !important;
  }
  .js-polish .panel .pr-draw {
    stroke-dasharray: none !important;
    stroke-dashoffset: 0 !important;
  }
  .kpi-card.pr-loading .kpi-value::after {
    animation: none !important;
    background: rgba(255, 255, 255, 0.07) !important;
  }
  .pr-ambient {
    animation: none !important;
    opacity: 1 !important;
    transform: none !important;
  }
}

/* ================================================================
   18. LAYOUT FIX — direct .panel children of .content can collapse
   ----------------------------------------------------------------
   `.content` is `display: flex; flex-direction: column`. Most
   panels are wrapped in `.panel-row` which sizes correctly, but a
   few (notably #plWaterfallPanel on dashboard.html) sit as direct
   `.panel` children of `.content`. In some layouts they collapse to
   ~1px because flex-shrink takes over when total children exceed
   container height. Force them to size to content.
   This is a pre-existing layout quirk surfaced by the polish review.
   ================================================================ */
.content > .panel {
  flex: 0 0 auto;
  min-height: fit-content;
}

/* ================================================================
   19. SCROLLBARS — distinguish main scroll from inner scroll regions
   ----------------------------------------------------------------
   Two scrollbars commonly appear stacked on the right edge:
     - .content (main page scroll)         → wider, neutral
     - .panel-body / .kd-body inner scroll → narrower, orange accent
   The accent makes it obvious which area you're scrolling.
   Hidden on mobile to keep the card-stack flow clean.
   ================================================================ */

/* Main content scroll — neutral / wider */
.content {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
}
.content::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
.content::-webkit-scrollbar-track {
  background: transparent;
}
.content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: padding-box;
  transition: background-color 160ms ease;
}
.content::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.28);
  background-clip: padding-box;
}

/* Inner panel scrolls — narrower, warm accent */
.panel-body,
.kd-body,
.panel-body *[class*="scroll"],
.kd-body *[class*="scroll"] {
  scrollbar-width: thin;
  scrollbar-color: rgba(242, 106, 46, 0.40) transparent;
}
.panel-body::-webkit-scrollbar,
.kd-body::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
.panel-body::-webkit-scrollbar-track,
.kd-body::-webkit-scrollbar-track {
  background: transparent;
}
.panel-body::-webkit-scrollbar-thumb,
.kd-body::-webkit-scrollbar-thumb {
  background: rgba(242, 106, 46, 0.32);
  border-radius: 3px;
  transition: background-color 160ms ease;
}
.panel-body::-webkit-scrollbar-thumb:hover,
.kd-body::-webkit-scrollbar-thumb:hover {
  background: rgba(242, 106, 46, 0.55);
}

/* Sidebar drawer (mobile) — same neutral as main scroll */
.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.10);
  border-radius: 3px;
}

/* Mobile: hide custom scrollbar styling to keep card-stack flow clean.
   iOS Safari already hides scrollbars by default on touch devices. */
@media (max-width: 640px) {
  .content::-webkit-scrollbar,
  .panel-body::-webkit-scrollbar,
  .kd-body::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
}

/* ================================================================
   20. P&L WATERFALL — investment-grade cascade bars
   ----------------------------------------------------------------
   The chart is rendered as SVG <g class="wf-bar wf-rev/wf-cost/wf-gp">
   blocks by renderPLWaterfall(). Hover brightens each bar.
   ================================================================ */
#plWaterfallSvg .wf-bar {
  transition: filter 180ms cubic-bezier(.4, 0, .2, 1);
  transform-box: fill-box;
  transform-origin: center;
}
#plWaterfallSvg .wf-bar:hover {
  filter: brightness(1.15) drop-shadow(0 4px 10px rgba(0, 0, 0, 0.4));
}
#plWaterfallSvg .wf-rev:hover  { filter: brightness(1.18) drop-shadow(0 4px 12px rgba(91, 168, 245, 0.30)); }
#plWaterfallSvg .wf-cost:hover { filter: brightness(1.15) drop-shadow(0 4px 12px rgba(229, 72, 77, 0.30)); }
#plWaterfallSvg .wf-gp:hover   { filter: brightness(1.18) drop-shadow(0 4px 12px rgba(39, 194, 140, 0.35)); }

/* Reduced motion: no filter transition */
@media (prefers-reduced-motion: reduce) {
  #plWaterfallSvg .wf-bar { transition: none !important; }
}

/* ================================================================
   END polish.css
   ---------------------------------------------------------------- */
