/* Prod 5.1 foundation (docs/DESIGN-STANDARD.md sections 1 to 5; macros in app/templates/theme/_p51.html).
   Presentation only. Every colour is a token below; light by default, dark under [data-theme="dark"] (the theme's own switch).
   Where a spec value fails 4.5:1 on one of the component backgrounds the token is darkened (muted, good, warn); tests/test_p51_foundation.py
   and the Playwright check hold every text/background pair to 4.5:1.
   Minimum widths: page head 280 px (actions wrap under the title); KPI tile 160 px (two to a row on a phone, 2x2);
   table wrapper any width (scrolls inside itself, first column sticky); callout and insight line 280 px (text wraps). */
:root {
  /* colour */
  --p51-ink: #1F2937;        --p51-muted: #5B6472;     --p51-brand: #1E5AA8;
  --p51-good: #166534;       --p51-bad: #B91C1C;       --p51-warn: #92400E;
  --p51-surface: #FFFFFF;    --p51-ground: #F5F7FA;
  --p51-row-alt: #F9FAFB;    --p51-row-hover: #EEF4FC; --p51-section: #F1F5F9;
  --p51-rule: #E5E7EB;       --p51-rule-strong: #9CA3AF; --p51-summary-bg: #F3F6FA;
  --p51-good-bg: #DCFCE7;    --p51-bad-bg: #FEE2E2;    --p51-warn-bg: #FEF3C7;   --p51-neutral-bg: #E5E7EB; --p51-neutral-fg: #374151;
  --p51-brand-bg: #E6EFFA;
  --p51-shadow: 0 1px 3px rgba(11, 37, 69, .10);
  /* type scale: 12 labels, 13 % rows and notes, 14 body, 16 section titles, 20 page title, 28 KPI values; the insight line is 19 */
  --p51-t-label: 12px; --p51-t-note: 13px; --p51-t-body: 14px; --p51-t-section: 16px; --p51-t-title: 20px; --p51-t-insight: 19px; --p51-t-kpi: 28px;
  /* spacing scale */
  --p51-s1: 4px; --p51-s2: 8px; --p51-s3: 12px; --p51-s4: 16px; --p51-s5: 24px; --p51-s6: 32px;
  --p51-row-h: 36px; --p51-radius: 10px;
}
[data-theme="dark"] {
  --p51-ink: #E5E7EB;        --p51-muted: #9CA3AF;     --p51-brand: #6EA8FE;
  --p51-good: #4ADE80;       --p51-bad: #F87171;       --p51-warn: #FBBF24;
  --p51-surface: #111827;    --p51-ground: #0B1220;
  --p51-row-alt: #151F32;    --p51-row-hover: #1E293B; --p51-section: #1F2937;
  --p51-rule: #374151;       --p51-rule-strong: #6B7280; --p51-summary-bg: #172033;
  --p51-good-bg: #0F3320;    --p51-bad-bg: #3B1D1D;    --p51-warn-bg: #3A2E0B;   --p51-neutral-bg: #374151; --p51-neutral-fg: #E5E7EB;
  --p51-brand-bg: #13294B;
  --p51-shadow: 0 1px 3px rgba(0, 0, 0, .5);
}
@media (max-width: 639px) { :root { --p51-row-h: 48px; } }

.p51-page { color: var(--p51-ink); font-size: var(--p51-t-body); max-width: 1440px; margin: 0 auto; display: grid; gap: var(--p51-s5); min-width: 0; }
.p51-page * { box-sizing: border-box; }

/* 1. slim header: small label, title, one sentence; source, as-of and at most two buttons on the right */
.p51-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: var(--p51-s3) var(--p51-s5); min-width: 0; }
.p51-head .p51-lab { margin: 0; font-size: var(--p51-t-label); font-weight: 600; letter-spacing: .5px; text-transform: uppercase; color: var(--p51-muted); }
.p51-head h1 { color: var(--p51-ink); margin: 2px 0 0; font-size: var(--p51-t-title); font-weight: 600; line-height: 1.25; overflow-wrap: anywhere; }
.p51-head .p51-sub { margin: 4px 0 0; color: var(--p51-muted); font-size: var(--p51-t-body); }
.p51-head .p51-meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--p51-s2) var(--p51-s3); font-size: var(--p51-t-label); color: var(--p51-muted); }
.p51-btn { display: inline-flex; align-items: center; justify-content: center; min-height: 32px; min-width: 32px; padding: 0 var(--p51-s3); border: 1px solid var(--p51-rule-strong); border-radius: 8px; background: var(--p51-surface); color: var(--p51-ink); font: inherit; font-size: var(--p51-t-body); font-weight: 600; text-decoration: none; cursor: pointer; }
.p51-btn:hover { background: var(--p51-row-hover); }
.p51-btn:focus-visible, .p51-tw:focus-visible, .p51-kpi:focus-visible, .p51-callout a:focus-visible, .p51-table a:focus-visible { outline: 2px solid var(--p51-brand); outline-offset: 2px; }

/* 1b. insight line */
.p51-insight { margin: 0; font-size: var(--p51-t-insight); line-height: 1.45; font-weight: 500; color: var(--p51-ink); max-width: 70ch; text-wrap: pretty; }

/* 2. KPI tiles: 3 or 4, equal width, two to a row on a phone; the hero is tinted with its meaning */
.p51-kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 160px), 1fr)); gap: var(--p51-s4); min-width: 0; }
.p51-kpi { display: flex; flex-direction: column; gap: var(--p51-s1); min-width: 0; padding: var(--p51-s4); background: var(--p51-surface); border: 1px solid var(--p51-rule); border-radius: var(--p51-radius); box-shadow: var(--p51-shadow); color: var(--p51-ink); text-decoration: none; }
a.p51-kpi:hover { background: var(--p51-row-hover); }
.p51-kpi .p51-k-l { font-size: var(--p51-t-label); font-weight: 600; letter-spacing: .5px; text-transform: uppercase; color: var(--p51-muted); overflow-wrap: anywhere; }
.p51-kpi .p51-k-v { font-size: var(--p51-t-kpi); font-weight: 600; line-height: 1.15; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.p51-kpi .p51-k-d { font-size: var(--p51-t-note); font-weight: 600; font-variant-numeric: tabular-nums; }
.p51-kpi .p51-k-d.good { color: var(--p51-good); } .p51-kpi .p51-k-d.bad { color: var(--p51-bad); } .p51-kpi .p51-k-d.flat { color: var(--p51-muted); }
.p51-kpi .p51-k-c { font-size: var(--p51-t-note); color: var(--p51-muted); }
.p51-kpi.hero { border-width: 2px; justify-content: flex-start; }   /* the app has its own .hero (navy, space-between): reset what leaks */
.p51-kpi.hero .p51-k-v { font-size: calc(var(--p51-t-kpi) + 8px); }
.p51-kpi.hero.bad { background: var(--p51-bad-bg); border-color: var(--p51-bad); } .p51-kpi.hero.bad .p51-k-l, .p51-kpi.hero.bad .p51-k-c { color: var(--p51-ink); }
.p51-kpi.hero.good { background: var(--p51-good-bg); border-color: var(--p51-good); } .p51-kpi.hero.good .p51-k-l, .p51-kpi.hero.good .p51-k-c { color: var(--p51-ink); }
.p51-kpi.hero.warn { background: var(--p51-warn-bg); border-color: var(--p51-warn); } .p51-kpi.hero.warn .p51-k-l, .p51-kpi.hero.warn .p51-k-c { color: var(--p51-ink); }
.p51-kpi.hero.brand { background: var(--p51-brand-bg); border-color: var(--p51-brand); } .p51-kpi.hero.brand .p51-k-l, .p51-kpi.hero.brand .p51-k-c { color: var(--p51-ink); }
.p51-kpi.hero.bad .p51-k-d.good, .p51-kpi.hero.warn .p51-k-d.good, .p51-kpi.hero.brand .p51-k-d.good { color: var(--p51-good); }
@container (min-width: 900px) { .p51-kpis.has-hero .p51-kpi.hero { grid-column: span 2; } }
.p51-kpis-wrap { container-type: inline-size; min-width: 0; }

/* 3. callout strip: one amber or red strip, each item a link to the row; never decorative */
.p51-callout { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--p51-s2) var(--p51-s4); padding: var(--p51-s3) var(--p51-s4); border-radius: var(--p51-radius); border: 1px solid var(--p51-warn); border-left-width: 6px; background: var(--p51-warn-bg); color: var(--p51-ink); font-size: var(--p51-t-body); min-width: 0; }
.p51-callout.bad { border-color: var(--p51-bad); background: var(--p51-bad-bg); }
.p51-callout .p51-c-t { font-weight: 700; color: var(--p51-ink); }
.p51-callout ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: var(--p51-s1) var(--p51-s4); min-width: 0; }
.p51-callout li { min-width: 0; overflow-wrap: anywhere; }
.p51-callout a { color: var(--p51-ink); font-weight: 600; text-decoration: underline; text-underline-offset: 2px; display: inline-block; min-height: 24px; }

/* status chips: text plus colour, same words everywhere */
.p51-chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; font-size: var(--p51-t-label); font-weight: 700; line-height: 1.4; white-space: nowrap; background: var(--p51-neutral-bg); color: var(--p51-neutral-fg); }
.p51-chip.overdue { background: var(--p51-bad-bg); color: var(--p51-bad); }
.p51-chip.due_soon, .p51-chip.stale { background: var(--p51-warn-bg); color: var(--p51-warn); }
.p51-chip.paid, .p51-chip.done { background: var(--p51-good-bg); color: var(--p51-good); }
.p51-chip.draft { background: var(--p51-neutral-bg); color: var(--p51-neutral-fg); }

/* 5. named sections and the table */
.p51-secttl { margin: 0 0 var(--p51-s2); font-size: var(--p51-t-section); font-weight: 600; color: var(--p51-ink); }
.p51-tblock { min-width: 0; container-type: inline-size; }
.p51-tools { display: flex; flex-wrap: wrap; align-items: center; gap: var(--p51-s2) var(--p51-s4); margin: 0 0 var(--p51-s2); font-size: var(--p51-t-label); color: var(--p51-muted); }
.p51-tools label { display: inline-flex; align-items: center; gap: 6px; min-height: 24px; cursor: pointer; }
.p51-tw { overflow: auto; max-height: min(70vh, 640px); border: 1px solid var(--p51-rule); border-radius: var(--p51-radius); background: var(--p51-surface); -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.p51-table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--p51-t-body); font-variant-numeric: tabular-nums; }
.p51-table th, .p51-table td { font-weight: 400; padding: 0 var(--p51-s3); height: var(--p51-row-h); border-bottom: 1px solid var(--p51-rule); white-space: nowrap; background: var(--p51-surface); color: var(--p51-ink); text-align: left; }
.p51-table thead th { position: sticky; top: 0; z-index: 3; background: var(--p51-section); color: var(--p51-muted); font-size: var(--p51-t-label); font-weight: 600; letter-spacing: .5px; text-transform: uppercase; border-bottom: 1px solid var(--p51-rule-strong); }
.p51-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.p51-table tr.section td.p51-first { position: static; max-width: none; overflow: visible; }
.p51-table th.p51-first, .p51-table td.p51-first { position: sticky; left: 0; z-index: 2; min-width: 140px; max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
.p51-table thead th.p51-first { z-index: 4; }
.p51-table tbody tr.p51-row.alt :is(td, th) { background: var(--p51-row-alt); }   /* zebra: plain data rows only (class set by build_table) */
.p51-table tbody tr.p51-row:hover :is(td, th) { background: var(--p51-row-hover); }
.p51-table td.neg { color: var(--p51-bad); } .p51-table td.zero { color: var(--p51-muted); }
.p51-table tr.section td { font-weight: 600; background: var(--p51-section); font-size: var(--p51-t-label); font-weight: 600; letter-spacing: .5px; text-transform: uppercase; color: var(--p51-ink); height: 32px; }
.p51-table tr.subtotal :is(td, th), .p51-table tr.total :is(td, th) { font-weight: 600; background: var(--p51-summary-bg); border-top: 1px solid var(--p51-rule-strong); }
.p51-table tr.grand :is(td, th) { font-weight: 700; background: var(--p51-summary-bg); border-top: 2px solid var(--p51-rule-strong); }
.p51-table tr.pct :is(td, th) { font-size: var(--p51-t-note); font-style: italic; color: var(--p51-muted); height: 28px; }
.p51-table tr.pct .p51-first { padding-left: 28px; }
.p51-table tr.pct td.neg { color: var(--p51-bad); }
.p51-table th.summary, .p51-table td.summary { border-left: 2px solid var(--p51-rule-strong); background: var(--p51-summary-bg); }
.p51-table tbody tr.p51-row td.summary { background: var(--p51-summary-bg); }   /* after the zebra and hover rules, same weight: the summary tint stays on every row */
.p51-table tbody tr.p51-row:hover td.summary { background: var(--p51-row-hover); }
.p51-table thead th.summary { background: var(--p51-section); }
.p51-table th.current, .p51-table td.current { box-shadow: inset 0 -2px 0 var(--p51-brand); }
.p51-table td.current { box-shadow: none; font-weight: 600; }
.p51-table .p51-empty { display: none; }
.p51-tblock:has(.p51-showempty:checked) .p51-empty { display: table-cell; }
.p51-flag { font-size: 11px; margin-right: 4px; font-weight: 700; }
.p51-flag.good { color: var(--p51-good); } .p51-flag.bad { color: var(--p51-bad); }
.p51-table a.drill { color: inherit; text-decoration: underline dotted; text-underline-offset: 3px; cursor: pointer; display: inline-block; min-height: 24px; line-height: var(--p51-row-h); }
.p51-table a.rowlink { color: var(--p51-brand); text-decoration: none; font-weight: 600; }
.p51-table a.rowlink:hover { text-decoration: underline; }
.p51-table .p51-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.p51-empty-state { padding: var(--p51-s5) var(--p51-s4); text-align: center; color: var(--p51-muted); background: var(--p51-surface); border: 1px dashed var(--p51-rule-strong); border-radius: var(--p51-radius); }

/* 6. footer legend: every mark on the page is explained here */
.p51-legend { margin: var(--p51-s3) 0 0; padding: 0; list-style: none; display: grid; gap: var(--p51-s1); font-size: var(--p51-t-note); color: var(--p51-muted); }
.p51-legend li { display: flex; gap: var(--p51-s2); align-items: baseline; min-width: 0; }
.p51-legend .p51-mk { flex: none; min-width: 52px; font-weight: 700; color: var(--p51-ink); font-variant-numeric: tabular-nums; }
.p51-tools input[type="checkbox"] { width: 24px; height: 24px; margin: 0; accent-color: var(--p51-brand); }
.p51-seclab { position: sticky; left: var(--p51-s3); display: inline-block; }

/* definitions reachable by tap: a small button (details/summary, no script) beside the label; the hover title stays */
.p51-tip { display: inline; }
.p51-tip summary { display: inline-flex; align-items: center; justify-content: center; vertical-align: middle; min-width: 24px; min-height: 24px; margin-left: 4px; list-style: none; cursor: pointer; color: var(--p51-brand); font-size: 0; }
.p51-tip summary::before { content: "i"; display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; border: 1px solid var(--p51-brand); border-radius: 999px; font-size: var(--p51-t-label); font-weight: 700; font-style: normal; text-transform: none; letter-spacing: 0; line-height: 1; }
.p51-tip summary::-webkit-details-marker { display: none; }
.p51-tip summary:focus-visible { outline: 2px solid var(--p51-brand); outline-offset: 2px; }
.p51-tip .p51-tip-text { display: block; white-space: normal; max-width: 36ch; padding: var(--p51-s1) 0 var(--p51-s2); font-size: var(--p51-t-note); font-weight: 400; font-style: normal; text-transform: none; letter-spacing: 0; color: var(--p51-ink); }
.p51-k-tip { display: none; font-size: var(--p51-t-note); color: var(--p51-muted); }
@media (hover: none) { .p51-k-tip { display: block; } }

/* phone: 48 px tap targets (CLAUDE.md section 7), the first table column capped so a number is always on screen */
@media (max-width: 639px) {
  .p51-btn { min-height: 48px; min-width: 48px; }
  .p51-tools label { min-height: 48px; min-width: 48px; }
  .p51-callout a { min-height: 48px; display: inline-flex; align-items: center; }
  .p51-table a.drill, .p51-table a.rowlink { min-height: 48px; display: inline-flex; align-items: center; }
  .p51-tip summary { min-height: 48px; min-width: 48px; }
  .p51-table th.p51-first, .p51-table td.p51-first { min-width: 0; width: 42cqw; max-width: 42cqw; }
}

/* Isolation from the app's global CSS (bca-theme.css), found by audit: .row {display:flex} (body rows are p51-row now), .hero {navy, flex, space-between}
   (reset on the tile), h1 {color: navy} (set on the title), .neg {danger colour} (td.neg and .p51-flag set their own), .num (font-variant only),
   a:hover {blue} (every p51 link states its own colour with more weight), input[type=checkbox] {18px} (sized above). */
.p51-table tr.p51-row, .p51-table thead tr { display: table-row; padding: 0; gap: 0; border-top: 0; justify-content: normal; align-items: normal; }

/* L5b: phone tap targets of the p51 finance pages are 48 px (CLAUDE.md section 7); !important because each page's own stylesheet sets 32 px controls at higher specificity */
@media (max-width: 639px) {
  .p51-page.fin400 .f-period select, .p51-page.fin400 .f-period .btn, .p51-page.fin400 .f-filter select, .p51-page.fin400 .f-filter input, .p51-page.fin400 .f-filter .btn,
  .p51-page.fin400 .f-data summary, .p51-page.fin400 .f-csv, .p51a-legacy .f-csv, .p51b-csv, .p51a-more, .p51a .p51a-data summary { min-height: 48px !important; display: inline-flex; align-items: center; }
  .p51-page.fin400 .f-data summary, .p51a .p51a-data summary { display: flex; }
}
.p51-page, .p51-page.p29, .p51-page .p29 { --ink-3: var(--p51-muted); --p29-ink-3: var(--p51-muted); }    /* L5b: grey hints inside a p51 page (the old sections and notes) were 4.43:1 on the page ground; muted is 5.5:1 and flips in dark */

/* L5b (visual review): the info icon sits in the tile's top-right corner on every width instead of taking a row of its own (a 48 px empty row between the label and the value on a phone); its text opens as a small card */
.p51-kpi { position: relative; }
.p51-kpi > .p51-k-l { padding-right: 44px; }
.p51-kpi > .p51-tip { position: absolute; top: 2px; right: 2px; margin: 0; }
.p51-kpi > .p51-tip .p51-tip-text { position: absolute; top: 100%; right: 0; z-index: 6; width: min(240px, 70vw); padding: var(--p51-s3); background: var(--p51-surface); border: 1px solid var(--p51-rule-strong); border-radius: 8px; box-shadow: var(--p51-shadow); text-align: left; }
.p51-kpi > .p51-tip summary { margin: 0; }
@media (max-width: 639px) { .ov2 .ov2c-btn { min-height: 44px !important; display: inline-flex; align-items: center; } }    /* L5b: Overview (owner) See list / Win-Back buttons were 35 px on a phone */

/* L5b (review B2, S-2): a link inside a table cell or a ranked row is a target of at least 24 px (44 px on a phone), whatever the page's own link style says */
.p51-page td a[href], .p51m td a[href], .p51m a.plain, .p51-page a.f-drill, .p51-page a.rowlink { display: inline-flex; align-items: center; min-height: 24px; min-width: 24px; }
@media (max-width: 639px) { .p51-page td a[href], .p51m td a[href], .p51m a.plain, .p51-page a.f-drill, .p51-page a.rowlink { min-height: 44px; min-width: 44px; } }
