/* Philentia Design System v1.0 — components
 * Copyright (c) 2026, IT Business Dominicana (ITBD)
 *
 * Primitives built only from the tokens. No hex values below this line.
 * Every class is prefixed `ph-` so it can sit inside the Frappe desk, inside a
 * web template, or inside a future SPA without colliding with any of them.
 */

/* ===========================================================================
 * Base
 * =========================================================================== */

.ph {
	font-family: var(--font-ui);
	font-size: var(--text-body);
	line-height: var(--leading-normal);
	color: var(--text-primary);
	-webkit-font-smoothing: antialiased;
}

/* Money, quantities and identifiers. Tabular figures keep decimal points in a
 * column; proportional digits make a ledger unreadable down the page. */
.ph-num {
	font-variant-numeric: tabular-nums;
	font-feature-settings: "tnum" 1;
}

.ph-brand-type {
	font-family: var(--font-brand);
	font-weight: var(--weight-bold);
	letter-spacing: -0.01em;
}

/* ===========================================================================
 * Focus — one ring, everywhere, and never removed without a replacement
 * =========================================================================== */

.ph :focus-visible,
.ph-focusable:focus-visible {
	outline: 2px solid var(--brand-primary);
	outline-offset: 2px;
	border-radius: var(--radius-xs);
}

/* ===========================================================================
 * Buttons
 * =========================================================================== */

.ph-btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: var(--space-2);
	min-height: var(--control-height);
	padding: 0 var(--space-5);
	border: 1px solid transparent;
	border-radius: var(--radius-md);
	font-family: var(--font-ui);
	font-size: var(--text-body);
	font-weight: var(--weight-semibold);
	line-height: 1;
	text-decoration: none;
	cursor: pointer;
	transition: background-color var(--duration-fast) var(--ease-out),
		border-color var(--duration-fast) var(--ease-out),
		box-shadow var(--duration-fast) var(--ease-out);
}

.ph-btn:disabled,
.ph-btn[aria-disabled="true"] {
	opacity: 0.5;
	cursor: not-allowed;
}

.ph-btn--primary {
	background: var(--brand-primary);
	color: var(--text-on-fill);
}
.ph-btn--primary:hover:not(:disabled) {
	background: var(--brand-primary-hover);
}

.ph-btn--secondary {
	background: var(--surface);
	border-color: var(--border-control);
	color: var(--text-secondary);
}
.ph-btn--secondary:hover:not(:disabled) {
	background: var(--surface-secondary);
}

/* Success is for an action that is explicitly positive — Approve, Activate.
 * It uses the *strong* green: white on `--success-fill` is 3.32:1 and fails AA.
 */
.ph-btn--success {
	background: var(--success-fill-strong);
	color: var(--text-on-fill);
}
.ph-btn--success:hover:not(:disabled) {
	background: var(--success-fill);
}

.ph-btn--danger {
	background: var(--danger-fill);
	color: var(--text-on-fill);
}
.ph-btn--danger:hover:not(:disabled) {
	background: var(--danger-fill-strong);
}
.ph-btn--danger:focus-visible {
	box-shadow: var(--focus-ring-danger);
}

.ph-btn--ghost {
	background: transparent;
	color: var(--text-link);
}
.ph-btn--ghost:hover:not(:disabled) {
	background: var(--philentia-blue-50);
}

.ph-btn--lg {
	min-height: 52px;
	padding: 0 var(--space-6);
	font-size: var(--text-body-lg);
}

/* Philentia AI has its own identity (§26). It is the one gradient control. */
.ph-btn--ai {
	background: var(--gradient-ai);
	color: var(--text-on-fill);
	border: none;
}
.ph-btn--ai:hover:not(:disabled) {
	filter: brightness(1.08);
}

/* ===========================================================================
 * Inputs
 * =========================================================================== */

.ph-field {
	display: flex;
	flex-direction: column;
	gap: var(--space-2);
}

.ph-label {
	font-size: var(--text-body);
	font-weight: var(--weight-medium);
	color: var(--text-secondary);
}

.ph-input,
.ph-select,
.ph-textarea {
	width: 100%;
	min-height: var(--control-height);
	padding: var(--space-2) var(--space-3);
	background: var(--surface);
	border: 1px solid var(--border-control);
	border-radius: var(--radius-md);
	font-family: var(--font-ui);
	font-size: var(--text-body);
	color: var(--text-primary);
	transition: border-color var(--duration-fast) var(--ease-out),
		box-shadow var(--duration-fast) var(--ease-out);
}

.ph-input::placeholder,
.ph-textarea::placeholder {
	color: var(--text-muted);
}

.ph-input:focus,
.ph-select:focus,
.ph-textarea:focus {
	outline: none;
	border-color: var(--border-focus);
	box-shadow: var(--focus-ring);
}

.ph-input[aria-invalid="true"] {
	border-color: var(--danger-fill);
}
.ph-input[aria-invalid="true"]:focus {
	box-shadow: var(--focus-ring-danger);
}

.ph-hint {
	font-size: var(--text-body-sm);
	color: var(--text-muted);
}
.ph-hint--error {
	color: var(--danger-text);
}

/* ===========================================================================
 * Cards
 * =========================================================================== */

.ph-card {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius-lg);
	padding: var(--space-5);
}

.ph-card--kpi {
	border-radius: var(--radius-xl);
	padding: var(--space-5);
}

.ph-card__label {
	font-size: var(--text-body);
	color: var(--text-muted);
	margin: 0 0 var(--space-2);
}

.ph-card__value {
	font-size: 1.875rem; /* 30px — inside the 28–32 band */
	font-weight: var(--weight-semibold);
	line-height: var(--leading-tight);
	color: var(--text-primary);
	margin: 0;
	font-variant-numeric: tabular-nums;
}

.ph-card__foot {
	display: flex;
	align-items: baseline;
	gap: var(--space-2);
	margin-top: var(--space-3);
	font-size: var(--text-body-sm);
	color: var(--text-muted);
}

/* A trend is never colour alone (§48): the arrow carries the same meaning for
 * somebody who cannot distinguish the green from the red. */
.ph-trend {
	display: inline-flex;
	align-items: center;
	gap: var(--space-1);
	font-weight: var(--weight-medium);
	font-variant-numeric: tabular-nums;
}
.ph-trend--up {
	color: var(--success-text);
}
.ph-trend--down {
	color: var(--danger-text);
}
.ph-trend--flat {
	color: var(--text-muted);
}

/* ===========================================================================
 * Badges — a dot plus a word, never a dot alone
 * =========================================================================== */

.ph-badge {
	display: inline-flex;
	align-items: center;
	gap: var(--space-2);
	height: 24px;
	padding: 0 var(--space-3);
	border: 1px solid transparent;
	border-radius: var(--radius-full);
	font-size: var(--text-caption);
	font-weight: var(--weight-medium);
	white-space: nowrap;
}

.ph-badge::before {
	content: "";
	width: 6px;
	height: 6px;
	border-radius: var(--radius-full);
	background: currentColor;
	flex: none;
}

.ph-badge--success {
	background: var(--success-bg);
	border-color: var(--success-border);
	color: var(--success-text);
}
.ph-badge--warning {
	background: var(--warning-bg);
	border-color: var(--warning-border);
	color: var(--warning-text);
}
.ph-badge--danger {
	background: var(--danger-bg);
	border-color: var(--danger-border);
	color: var(--danger-text);
}
.ph-badge--info {
	background: var(--info-bg);
	border-color: var(--info-border);
	color: var(--info-text);
}
.ph-badge--muted {
	background: var(--muted-bg);
	border-color: var(--muted-border);
	color: var(--muted-text);
}

/* ===========================================================================
 * Progress
 * =========================================================================== */

.ph-progress {
	height: 10px;
	background: var(--neutral-200);
	border-radius: var(--radius-full);
	overflow: hidden;
}

.ph-progress__bar {
	display: block;
	height: 100%;
	border-radius: var(--radius-full);
	background: var(--brand-primary);
	transition: width var(--duration-slow) var(--ease-out);
}

/* Fundraising: money on the left becoming outcome on the right (§35). */
.ph-progress__bar--fundraising {
	background: var(--gradient-progress);
}
.ph-progress__bar--impact {
	background: var(--gradient-impact);
}

.ph-progress__meta {
	display: flex;
	justify-content: space-between;
	gap: var(--space-3);
	margin-top: var(--space-2);
	font-size: var(--text-body-sm);
	color: var(--text-muted);
}

/* ===========================================================================
 * Tables
 * =========================================================================== */

.ph-table-wrap {
	overflow-x: auto;
	border: 1px solid var(--border);
	border-radius: var(--radius-lg);
}

.ph-table {
	width: 100%;
	border-collapse: collapse;
	font-size: var(--text-body);
}

.ph-table th {
	position: sticky;
	top: 0;
	z-index: 1;
	background: var(--surface-secondary);
	text-align: left;
	padding: var(--space-3);
	font-size: var(--text-caption);
	font-weight: var(--weight-medium);
	text-transform: uppercase;
	letter-spacing: 0.04em;
	color: var(--text-muted);
	border-bottom: 1px solid var(--border);
	white-space: nowrap;
}

.ph-table td {
	padding: var(--space-3);
	border-bottom: 1px solid var(--border);
	color: var(--text-secondary);
	vertical-align: middle;
}

.ph-table tbody tr:last-child td {
	border-bottom: none;
}

.ph-table tbody tr:hover td {
	background: var(--surface-secondary);
}

/* Same failure, different element: a long identifier in a cell widens the
 * column until the table scrolls sideways for one row. */
.ph-table td {
	overflow-wrap: anywhere;
}

.ph-table .ph-num,
.ph-table td[data-numeric],
.ph-table th[data-numeric] {
	text-align: right;
	font-variant-numeric: tabular-nums;
}

/* ===========================================================================
 * Timelines (§39 audit, §40 compliance)
 * =========================================================================== */

.ph-timeline {
	list-style: none;
	margin: 0;
	padding: 0 0 0 var(--space-6);
	border-left: 2px solid var(--border);
}

.ph-timeline__item {
	position: relative;
	padding: 0 0 var(--space-6) var(--space-5);
}
.ph-timeline__item:last-child {
	padding-bottom: 0;
}

.ph-timeline__item::before {
	content: "";
	position: absolute;
	left: calc(var(--space-6) * -1 - 7px);
	top: 4px;
	width: 12px;
	height: 12px;
	border-radius: var(--radius-full);
	background: var(--surface);
	border: 2px solid var(--brand-primary);
}

/* A step not yet taken is hollow and grey. Done is filled. The difference is
 * shape as well as colour. */
.ph-timeline__item--pending::before {
	border-color: var(--border-strong);
}
.ph-timeline__item--done::before {
	background: var(--brand-primary);
}
.ph-timeline__item--success::before {
	background: var(--success-fill-strong);
	border-color: var(--success-fill-strong);
}

.ph-timeline__when {
	font-size: var(--text-caption);
	color: var(--text-muted);
	text-transform: uppercase;
	letter-spacing: 0.04em;
}

.ph-timeline__what {
	font-weight: var(--weight-medium);
	color: var(--text-primary);
	margin: var(--space-1) 0 0;
}

.ph-timeline__who {
	font-size: var(--text-body-sm);
	color: var(--text-muted);
}

/* ===========================================================================
 * Why does this apply? (§41)
 *
 * The component that separates Philentia from a spreadsheet: an obligation is
 * never asserted without the authority, the article and the version behind it.
 * =========================================================================== */

.ph-source {
	background: var(--philentia-blue-50);
	border: 1px solid var(--info-border);
	border-radius: var(--radius-lg);
	padding: var(--space-4);
}

.ph-source__title {
	font-size: var(--text-caption);
	font-weight: var(--weight-semibold);
	text-transform: uppercase;
	letter-spacing: 0.05em;
	color: var(--info-text);
	margin: 0 0 var(--space-3);
}

.ph-source__grid {
	display: grid;
	/* `minmax(0, ...)` not `minmax(140px, ...)`: the 140px floor is what let a
	 * long identifier push a track wider than its share. The floor moves to
	 * the media query instead, where it belongs. */
	grid-template-columns: repeat(2, minmax(0, 1fr));
	gap: var(--space-4) var(--space-5);
	margin: 0;
}

@media (min-width: 768px) {
	.ph-source__grid {
		grid-template-columns: repeat(3, minmax(0, 1fr));
	}
}

/* A field whose value is a long identifier takes the whole row. Breaking at a
 * separator is much better than breaking mid-token, but not breaking at all is
 * better still, and a rule id is the field somebody copies. */
.ph-source__grid > .ph-source__field--wide {
	grid-column: 1 / -1;
}

.ph-source__grid dt {
	font-size: var(--text-caption);
	color: var(--text-muted);
	text-transform: uppercase;
	letter-spacing: 0.04em;
}

.ph-source__grid dd {
	margin: var(--space-1) 0 0;
	font-size: var(--text-body);
	font-weight: var(--weight-medium);
	color: var(--text-primary);
	/* `anywhere` rather than `break-word`: it lets the browser break inside the
	 * token *and* shrinks the min-content contribution, which is the half that
	 * actually stops the blow-out. */
	overflow-wrap: anywhere;
}

/* Verified and unverified must not look alike. An unverified rule that renders
 * like a verified one is the single worst thing this component could do. */
.ph-source--unverified {
	background: var(--warning-bg);
	border-color: var(--warning-border);
}
.ph-source--unverified .ph-source__title {
	color: var(--warning-text);
}

/* ===========================================================================
 * Empty states (§43) and loading (§44)
 * =========================================================================== */

.ph-empty {
	text-align: center;
	padding: var(--space-16) var(--space-6);
	border: 1px dashed var(--border-strong);
	border-radius: var(--radius-lg);
}

.ph-empty__title {
	font-size: var(--text-h4);
	font-weight: var(--weight-semibold);
	color: var(--text-primary);
	margin: 0 0 var(--space-2);
}

.ph-empty__body {
	color: var(--text-muted);
	max-width: 42ch;
	margin: 0 auto var(--space-5);
	line-height: var(--leading-relaxed);
}

.ph-skeleton {
	background: linear-gradient(
		90deg,
		var(--neutral-100) 25%,
		var(--neutral-200) 37%,
		var(--neutral-100) 63%
	);
	background-size: 400% 100%;
	border-radius: var(--radius-sm);
	animation: ph-shimmer 1.4s ease-in-out infinite;
}

@keyframes ph-shimmer {
	0% {
		background-position: 100% 50%;
	}
	100% {
		background-position: 0 50%;
	}
}

@media (prefers-reduced-motion: reduce) {
	.ph-skeleton {
		animation: none;
	}
}

/* ===========================================================================
 * Layout helpers
 * =========================================================================== */

.ph-grid {
	display: grid;
	gap: var(--space-4);
}

/* A grid item defaults to `min-width: auto`, which means its *minimum content
 * size* — and an unbreakable string like `DO.Q.ART50.COMMERCIAL_BASIS` has a
 * min-content width as wide as the whole token. The track then grows past its
 * `1fr` share and the content escapes the card.
 *
 * This is the single most common CSS grid defect and it only shows up when
 * real data arrives: every identifier in this product is a long unbroken
 * token, so it shows up constantly. Declared once, here, rather than patched
 * per component. */
.ph-grid > *,
.ph-source__grid > *,
.ph-stack > * {
	min-width: 0;
}
.ph-grid--kpi {
	grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
.ph-grid--2 {
	grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

.ph-stack {
	display: flex;
	flex-direction: column;
	gap: var(--space-4);
}

.ph-row {
	display: flex;
	align-items: center;
	gap: var(--space-3);
	flex-wrap: wrap;
}

/* Machine identifiers — rule ids, award numbers, RNC, e-CF codes. Monospace
 * because they are read character by character and confused otherwise (0/O,
 * 1/l), and breakable because they have no spaces to break at. */
.ph-code {
	font-family: var(--font-mono);
	font-size: 0.9375em;
	letter-spacing: -0.01em;
	/* Last resort only. Per spec `anywhere` breaks inside a word *when there
	 * is no other acceptable break point on the line*, so a `<wbr>` from
	 * `breakable_code` is preferred and the ugly mid-token split only happens
	 * when a separator alone cannot save the line. Keeping it is what
	 * guarantees the text can never escape its container. */
	overflow-wrap: anywhere;
}

.ph-sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}
