/* ── 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-size: 0.72rem;
  white-space: nowrap;
}

/* The 19% target next to the DB1 percentage — context, not a second figure. */
.management-target {
  font-weight: 500;
  color: var(--gray-mid);
}

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

.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(--teal);
  text-decoration: underline;
  cursor: pointer;
}

.management-refresh-button:hover {
  color: var(--teal-dark);
}

/* Joined segmented control (per the mock-up): the shared .toggle-pill
   without its checkbox square. */
.donor-toggles {
  gap: 0;
}

.donor-toggle {
  position: relative; /* lets the active segment's border win the -1px overlap */
  border-radius: 0;
  margin-left: -1px;
  padding: 0.45rem 0.9rem;
  font-weight: 600;
}

.donor-toggle::before { display: none; }

.donor-toggle:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.donor-toggle:last-child  { border-radius: 0 6px 6px 0; }

.donor-toggle.is-active {
  background: var(--teal);
  border-color: var(--teal);
  color: var(--white);
  z-index: 1;
}

.donor-toggle.is-active:hover { color: var(--white); }

/* The period picker: the shared month grid (month_grid.css) inside the
   filter-bar dropdown (filters.css). It sits mid-bar, so it drops the
   dropdown's push-right and the field label's uppercase, and anchors left. */
.period-filter {
  margin-left: 0;
}

.period-filter .filter-more-trigger {
  display: inline-block;
  text-transform: none;
  letter-spacing: 0;
}

.period-filter-panel {
  left: 0;
  right: auto;
  width: calc(420px + 2rem);
}

.period-filter-hint {
  margin: 0;
  font-size: 0.8rem;
  color: var(--gray-mid);
  line-height: 1.4;
  text-transform: none;
  letter-spacing: 0;
}

.period-filter-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* The .filter-field labels above push the control row down — align the
   reset link with the controls, not the labels. */
.management-clear-filters {
  margin-left: auto;
  align-self: flex-end;
  padding-bottom: 0.65rem;
}

/* 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%; }
}

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

.column-picker-hint {
  margin: 0 0 0.25rem;
  font-size: 0.78rem;
  color: var(--gray-mid);
}

.column-picker-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--gray-dark);
  cursor: pointer;
  white-space: nowrap;
}

/* Donor-group cards (per the mock-up): one card per group plus a highlighted
   Total, each with the group's DB1 % headline, its euro base beneath, and the
   euro distance to the target. */
.donor-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  gap: 1rem;
}

.donor-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  background: var(--white);
}

.donor-card--total {
  border-color: var(--teal);
}

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

.donor-card-name {
  font-weight: 600;
  color: var(--text-strong);
}

.donor-card-pct {
  font-size: 1.15rem;
  font-weight: 700;
}

.donor-card-line {
  font-size: 0.85rem;
  color: var(--gray-mid);
  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. */
.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(--divider);
  font-size: 0.9rem;
}

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

.by-month-head {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--gray-mid);
  border-bottom: 1px solid var(--gray-light);
}

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

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

.by-month-name::before {
  content: "▸ ";
  color: var(--gray-mid);
}

.by-month-month[open] .by-month-name::before { content: "▾ "; }

.by-month-month[open] > summary {
  background: var(--surface-muted);
}

.by-month-project {
  padding-left: 1.75rem;
  font-size: 0.85rem;
  color: var(--gray-dark);
}

.by-month-empty {
  margin: 0;
  padding: 0.4rem 1.75rem;
  font-size: 0.85rem;
  color: var(--gray-mid);
  border-bottom: 1px solid var(--divider);
}

.by-month-total {
  font-weight: 600;
  color: var(--text-strong);
  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(--white);
}

/* 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(--surface-muted);
  box-shadow: inset 0 -1px 0 var(--gray-light);
}

/* 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(--divider);
}

/* The opaque background must follow the row hover. */
.management-projects-table tbody tr:hover td[data-column="code"],
.management-projects-table tbody tr:hover td[data-column="project"] {
  background: var(--surface-muted);
}

/* 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-size: 0.72rem;
  font-weight: 500;
  color: var(--gray-mid);
  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-size: 0.78rem;
  color: var(--gray-mid);
}

.management-legend-title {
  font-weight: 600;
  color: var(--text-strong);
}

.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: 2px;
  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. */
.target-chip {
  display: inline-block;
  margin-top: 0.5rem;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}

.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. */
.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: rgba(17, 24, 39, 0.35);
}

.kpi-panel-sheet {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  width: min(30rem, 92vw);
  background: var(--white);
  box-shadow: -8px 0 30px rgba(0, 0, 0, 0.15);
}

.kpi-panel[open] .kpi-panel-sheet {
  animation: kpi-panel-in 0.22s ease;
}

@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(--gray-mid);
}

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

.kpi-panel-head {
  padding: 1.1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-muted);
}

.kpi-panel-head h2 {
  margin: 0;
  font-size: 1.05rem;
  color: var(--text-strong);
  padding-right: 1.5rem; /* clears the close button */
}

.kpi-panel-close {
  float: right;
  border: 0;
  padding: 0;
  background: none;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--gray-mid);
  cursor: pointer;
}

.kpi-panel-sub {
  margin: 0.2rem 0 0;
  font-size: 0.82rem;
  color: var(--gray-mid);
}

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

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

.kpi-group {
  margin: 1.1rem 0 0.55rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--divider);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--teal-dark);
}

.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(--text-strong);
}

.kpi-cell-label {
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--gray-mid);
}

.kpi-cell-value {
  margin-top: 0.15rem;
  font-size: 1.05rem;
  font-weight: 700;
  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(--divider);
  font-size: 0.85rem;
}

/* The panel's tips are all rendered align: :left (right-anchored), which is
   right for the grid's right column. Flip the LEFT column rightward, or the
   scrolling body clips the bubble against the sheet's left edge. */
.kpi-grid .kpi-cell:nth-child(odd) .info-tip::after {
  left: 0;
  right: auto;
}
.kpi-grid .kpi-cell:nth-child(odd) .info-tip::before {
  left: 4px;
  right: auto;
}

.kpi-panel .info-tip::after {
  max-width: 220px; /* base.css's 260px can cross the narrow sheet */
}

/* One column on a narrow screen; every marker then sits left, so every bubble
   opens rightward. */
@media (max-width: 30rem) {
  .kpi-grid { grid-template-columns: 1fr; }

  .kpi-grid .kpi-cell .info-tip::after {
    left: 0;
    right: auto;
  }
  .kpi-grid .kpi-cell .info-tip::before {
    left: 4px;
    right: auto;
  }
}
