/* ── Management view (cross-project finance overview) ──────────────────────
   Reuses the controlling summary cells (.controlling-summary*, controlling.css),
   the shared table component (tables.css) and the donor badges (projects.css);
   only what is specific to this page lives here. */

/* A grid, not the drill-down's wrapping flex row (controlling.css), so the
   figures line up in columns across rows. Top-aligned: a wrapped value must
   not push its neighbours off the shared baseline. */
.management-summary .controlling-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
  gap: 1rem 2rem;
  align-items: start;
}

/* Subdued annotations beside a summary figure — shared metrics so the two
   can't drift apart when they sit in the same cell. */
.management-target,
.management-subvalue {
  margin-left: 0.4rem;
  font: var(--md-sys-typescale-label-small);
  letter-spacing: var(--md-sys-typescale-label-small-tracking);
  white-space: nowrap;
}

/* The 19% target next to the DB1 percentage — context, not a second figure. */
.management-target {
  color: var(--md-sys-color-on-surface-variant);
}

/* A percentage's euro amount — it carries a figure rather than context, so
   margin_class may colour it. */
.management-subvalue {
  font-weight: 500;
}

.management-alltime-note {
  margin: 0.5rem 0 1rem;
}

/* The stored-figures stamp under the all-time note, the refresh button
   inline at its end. */
.management-figures-stamp {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin: -0.5rem 0 1rem;
}

.management-figures-stamp form {
  display: inline;
}

.management-refresh-button {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--md-sys-color-primary);
  text-decoration: underline;
  cursor: pointer;
}

.management-refresh-button:hover {
  color: var(--md-sys-color-on-primary-container);
}

/* Projects heading with the columns picker at its right. */
.management-table-head {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.management-table-head h2 {
  margin-bottom: 0;
}

/* filters.css makes the panel static here, so the picker takes its own line. */
@media (max-width: 760px) {
  .management-table-head { flex-wrap: wrap; }
  .management-table-head .column-picker { flex-basis: 100%; }
}

/* The picker's chip sits at the heading's right edge, so its menu opens
   leftward from there. */
.management-table-head .column-picker { margin-left: auto; }

.column-picker-panel {
  left: auto;
  right: 0;
  gap: 0.45rem;
  max-height: 60vh;
  overflow: auto;
}

.column-picker-hint {
  margin: 0 0 0.25rem;
  font: var(--md-sys-typescale-body-small);
  letter-spacing: var(--md-sys-typescale-body-small-tracking);
  color: var(--md-sys-color-on-surface-variant);
}

.column-picker-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font: var(--md-sys-typescale-body-medium);
  letter-spacing: var(--md-sys-typescale-body-medium-tracking);
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  white-space: nowrap;
}

/* Donor-group cards: one white outlined card per group plus the Total, each
   with the group's DB1 % headline, its euro base beneath, and the euro
   distance to the target. They sit on the page, not in a section card. */
.donor-overview { margin-bottom: 1.5rem; }

.donor-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  gap: 1rem;
}

.donor-card {
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-sys-shape-corner-medium);
  padding: 1rem 1.25rem;
  background: var(--md-sys-color-surface-container-lowest);
}

/* The Total is marked by a primary outline, not a fill: a primary-container
   fill was the loudest surface on the page, and the margin's error/amber/green
   colours aren't M3 pairs on it. Same highlight as a Flexible Remuneration
   card in the split wizard. */
.donor-card--total {
  border-color: var(--md-sys-color-primary);
}

.donor-card-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.donor-card-name {
  font: var(--md-sys-typescale-title-small);
  letter-spacing: var(--md-sys-typescale-title-small-tracking);
  color: var(--md-sys-color-on-surface);
}

.donor-card-pct {
  font: var(--md-sys-typescale-headline-small);
  letter-spacing: var(--md-sys-typescale-headline-small-tracking);
  font-variant-numeric: tabular-nums;
}

.donor-card-line {
  font: var(--md-sys-typescale-body-medium);
  letter-spacing: var(--md-sys-typescale-body-medium-tracking);
  color: var(--md-sys-color-on-surface-variant);
  margin: 0.15rem 0;
}

/* ---- By month --------------------------------------------------------------
   One grid row per month; a <details> opens the month to its projects, so the
   rows are grid lines, not a table. Six columns need ~50rem, so on a narrower
   screen the card scrolls sideways, like the app's tables. */
.by-month { overflow-x: auto; }

.by-month-row {
  display: grid;
  grid-template-columns: minmax(10rem, 2fr) repeat(5, minmax(7rem, 1fr));
  gap: 0.5rem 1rem;
  align-items: baseline;
  padding: 0.5rem 0.25rem;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  font: var(--md-sys-typescale-body-medium);
  letter-spacing: var(--md-sys-typescale-body-medium-tracking);
}

.by-month-row .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.by-month-head {
  font: var(--md-sys-typescale-title-small);
  letter-spacing: var(--md-sys-typescale-title-small-tracking);
  color: var(--md-sys-color-on-surface-variant);
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}

.by-month-month > summary {
  cursor: pointer;
  list-style: none;
}

.by-month-month > summary::-webkit-details-marker { display: none; }

.by-month-name::before {
  content: "";
  display: inline-block;
  width: 18px;
  height: 18px;
  margin-right: 0.25rem;
  vertical-align: -4px;
  background-color: var(--md-sys-color-on-surface-variant);
  -webkit-mask: var(--md-icon-chevron-right) center / contain no-repeat;
  mask: var(--md-icon-chevron-right) center / contain no-repeat;
  transition: transform var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-standard);
}

.by-month-month[open] .by-month-name::before { transform: rotate(90deg); }

.by-month-month > summary:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent);
}

.by-month-month[open] > summary {
  background: var(--md-sys-color-surface-container-low);
}

.by-month-project {
  padding-left: 1.75rem;
  font: var(--md-sys-typescale-body-medium);
  letter-spacing: var(--md-sys-typescale-body-medium-tracking);
  color: var(--md-sys-color-on-surface);
}

.by-month-empty {
  margin: 0;
  padding: 0.4rem 1.75rem;
  font: var(--md-sys-typescale-body-medium);
  letter-spacing: var(--md-sys-typescale-body-medium-tracking);
  color: var(--md-sys-color-on-surface-variant);
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}

.by-month-total {
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
  border-bottom: 0;
}

/* The project table is the end of the page, so it grows with the page rather
   than scrolling inside tables.css's viewport-height cap. The wrap still
   scrolls sideways for the wide KPI columns. */
.management-projects-wrap {
  max-height: none;
}

/* Pin the identity columns while the wide KPI table scrolls sideways. Project
   is offset by exactly Code's width, so the two share one variable — a `left`
   larger than the real width leaves a gap the KPI cells slide through, a
   smaller one paints Project over the code. */
.management-projects-table {
  --pinned-code-width: 6rem;
}

.management-projects-table th[data-column="code"],
.management-projects-table td[data-column="code"],
.management-projects-table th[data-column="project"],
.management-projects-table td[data-column="project"] {
  position: sticky;
  z-index: 1;
  background: var(--md-sys-color-surface-container-lowest);
}

/* Clamped both ways, not a floor: project codes are free text, so a long one
   would widen this column and overlap Project, while `anywhere` (which lets a
   long code wrap instead of forcing the column wider) would otherwise let
   auto table layout shrink it below the offset and open a gap. */
.management-projects-table th[data-column="code"],
.management-projects-table td[data-column="code"] {
  left: 0;
  min-width: var(--pinned-code-width);
  width: var(--pinned-code-width);
  max-width: var(--pinned-code-width);
  overflow-wrap: anywhere;
}

.management-projects-table th[data-column="project"],
.management-projects-table td[data-column="project"] {
  left: var(--pinned-code-width);
}

/* Pinned on both axes, so above the other sticky headers (z 2). */
.management-projects-table thead th[data-column="code"],
.management-projects-table thead th[data-column="project"] {
  z-index: 3;
  background: var(--md-sys-color-surface-container-low);
  box-shadow: inset 0 -1px 0 var(--md-sys-color-outline-variant);
}

/* Collapsed borders are painted by the table, not the cell, so they stay
   behind while a sticky cell moves — restate the row divider as an inset
   shadow, which travels with the cell. */
.management-projects-table tbody tr:not(:last-child) td[data-column="code"],
.management-projects-table tbody tr:not(:last-child) td[data-column="project"] {
  box-shadow: inset 0 -1px 0 var(--md-sys-color-outline-variant);
}

/* The opaque background must follow the row hover (an 8% on-surface state
   layer, flattened onto the cell's own container because the cell is opaque). */
.management-projects-table tbody tr:hover td[data-column="code"],
.management-projects-table tbody tr:hover td[data-column="project"] {
  background: color-mix(in srgb, var(--md-sys-color-surface-container-lowest), var(--md-sys-color-on-surface) 8%);
}

/* The subdued context line under a table figure — the period a timeliness day
   count belongs to, the still-open reading, the "no period derived" reason, the
   left-behind record count and budget utilization's cost/expected readings.
   Context, not second figures. Uncoloured: a long delay or a left-behind item
   can be the correct call. */
.timeliness-note {
  margin-top: 0.1rem;
  font: var(--md-sys-typescale-label-small);
  letter-spacing: var(--md-sys-typescale-label-small-tracking);
  color: var(--md-sys-color-on-surface-variant);
  white-space: nowrap;
}

/* Traffic-light figures, shared by the cards, the headline strip and the project
   table (bands in ManagementHelper). The table selectors are NOT redundant:
   .data-table tbody td (tables.css) sets its own grey and outranks a lone class,
   so without them the rows stay grey while the cards colour correctly. */
.margin-pct--above,
.uplift-pp--above,
.management-projects-table tbody td.margin-pct--above,
.management-projects-table tbody td.uplift-pp--above { color: var(--success); }

.margin-pct--near,
.uplift-pp--near,
.management-projects-table tbody td.margin-pct--near,
.management-projects-table tbody td.uplift-pp--near { color: var(--warning); }

.margin-pct--below,
.uplift-pp--below,
.management-projects-table tbody td.margin-pct--below,
.management-projects-table tbody td.uplift-pp--below { color: var(--danger); }

/* Legend for the colours above. */
.management-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 1rem;
  margin-top: 1rem;
  font: var(--md-sys-typescale-body-small);
  letter-spacing: var(--md-sys-typescale-body-small-tracking);
  color: var(--md-sys-color-on-surface-variant);
}

.management-legend-title {
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
}

.management-legend-key {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  white-space: nowrap;
}

.management-legend-swatch {
  width: 0.6rem;
  height: 0.6rem;
  border-radius: var(--md-sys-shape-corner-extra-small);
  background: currentColor;
}

/* Wraps to its own line on a narrow viewport. */
.management-legend-scales {
  flex: 1 1 20rem;
}

/* The "€x above/below target" chip on a donor card, led by an arrow. */
.target-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  margin-top: 0.5rem;
  padding: 0.15rem 0.6rem;
  border-radius: var(--md-sys-shape-corner-small);
  font: var(--md-sys-typescale-label-medium);
  letter-spacing: var(--md-sys-typescale-label-medium-tracking);
}

.target-chip .icon { width: 14px; height: 14px; vertical-align: 0; }

.target-chip--above {
  color: var(--success);
  background: var(--success-bg);
}

.target-chip--below {
  color: var(--danger);
  background: var(--danger-bg);
}

/* ---- Per-project KPI slide-over ---------------------------------------------
   A <dialog> stretched transparent over the viewport, so a click on the dialog
   itself is a scrim click (kpi_panel_controller.js); the visible sheet is
   anchored to the right edge — an M3 modal side sheet. */
.kpi-panel {
  position: fixed;
  inset: 0;
  width: 100%;
  max-width: none;
  height: 100%;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
}

.kpi-panel::backdrop {
  background: rgb(0 0 0 / 0.32);
}

.kpi-panel-sheet {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  /* M3 side sheets top out at 400dp. */
  width: min(25rem, calc(100vw - 3.5rem));
  background: var(--md-sys-color-surface-container-low);
  border-radius: var(--md-sys-shape-corner-large) 0 0 var(--md-sys-shape-corner-large);
  box-shadow: var(--md-sys-elevation-level3);
}

.kpi-panel[open] .kpi-panel-sheet {
  animation: kpi-panel-in var(--md-sys-motion-duration-medium2) var(--md-sys-motion-easing-emphasized-decelerate);
}

@keyframes kpi-panel-in {
  from { transform: translateX(100%); }
  to   { transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  .kpi-panel[open] .kpi-panel-sheet { animation: none; }
}

/* Hidden while Turbo loads a project (not dimmed): the previous project's
   figures must not sit in the panel while another one is on its way. */
.kpi-panel-frame {
  display: flex;
  flex-direction: column;
  /* flex + min-height:0 down the chain lets .kpi-panel-body scroll. */
  flex: 1;
  min-height: 0;
}

.kpi-panel-frame[aria-busy="true"] { display: none; }

.kpi-panel-loading {
  display: none;
  margin: 0;
  padding: 1.1rem 1.25rem;
  color: var(--md-sys-color-on-surface-variant);
}

.kpi-panel-frame[aria-busy="true"] ~ .kpi-panel-loading { display: block; }

/* M3 side-sheet header: title-large headline with the close icon button on
   the trailing edge, 24dp padding, a divider above the scrolling body. */
.kpi-panel-head {
  padding: 1.5rem 1.5rem 1rem;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  border-top-left-radius: var(--md-sys-shape-corner-large);
  background: var(--md-sys-color-surface-container-low);
}

.kpi-panel-head h2 {
  margin: 0;
  font: var(--md-sys-typescale-title-large);
  letter-spacing: var(--md-sys-typescale-title-large-tracking);
  color: var(--md-sys-color-on-surface);
  padding-right: 2.5rem; /* clears the close button */
}

.kpi-panel-close {
  float: right;
  margin: -0.5rem -0.75rem 0 0;
}

.kpi-panel-sub {
  margin: 0.2rem 0 0;
  font: var(--md-sys-typescale-body-medium);
  letter-spacing: var(--md-sys-typescale-body-medium-tracking);
  color: var(--md-sys-color-on-surface-variant);
}

.kpi-panel-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem 1.5rem 1.5rem;
}

.kpi-panel-period {
  margin-top: 0;
}

.kpi-group {
  margin: 1.1rem 0 0.55rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  font: var(--md-sys-typescale-title-small);
  letter-spacing: var(--md-sys-typescale-title-small-tracking);
  color: var(--md-sys-color-primary);
}

.kpi-group:first-child { margin-top: 0; }

.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem 1rem;
}

/* The value colour is set HERE, not on .kpi-cell-value: as a single class it
   would tie with (and, loading later, beat) .margin-negative and
   .db1-pct--below, so every traffic light in the panel would render near-black.
   Inherited from the parent, a state class on the value still wins. */
.kpi-cell {
  display: flex;
  flex-direction: column;
  min-width: 0; /* long labels wrap instead of widening the column */
  color: var(--md-sys-color-on-surface);
}

.kpi-cell-label {
  font: var(--md-sys-typescale-label-medium);
  letter-spacing: var(--md-sys-typescale-label-medium-tracking);
  color: var(--md-sys-color-on-surface-variant);
}

.kpi-cell-value {
  margin-top: 0.15rem;
  font: var(--md-sys-typescale-title-medium);
  letter-spacing: var(--md-sys-typescale-title-medium-tracking);
  font-variant-numeric: tabular-nums;
}

/* Panel sub-lines reuse .timeliness-note (the app's context line under a
   figure); only its table-oriented nowrap has to go, since a panel cell is half
   a 30rem sheet. */
.kpi-cell .timeliness-note {
  white-space: normal;
}

.kpi-panel-actions {
  margin: 1.4rem 0 0;
  padding-top: 0.9rem;
  border-top: 1px solid var(--md-sys-color-outline-variant);
  font: var(--md-sys-typescale-body-medium);
  letter-spacing: var(--md-sys-typescale-body-medium-tracking);
}

/* One column on a narrow screen. */
@media (max-width: 30rem) {
  .kpi-grid { grid-template-columns: 1fr; }
}
