/* ==========================================================================
   CoF Data Table — front end + editor
   Desktop: a normal table. The theme's table styles (e.g. .is-style-blue-table)
   apply because the wrapper also carries .wp-block-table.
   Small screens: .cof-dt--stack (cards) or .cof-dt--scroll (sideways scroll).
   Breakpoints (sm = 600px, md = 767px, lg = 1024px) are screen widths (media queries),
   matching the "Switch at screen width" setting, so desktop shows a table and the editor's
   Tablet / Mobile previews show the mobile layout. A table in a narrow column on a wide screen
   keeps table form and scrolls inside its own box if it doesn't fit.
   ========================================================================== */

.wp-block-cof-data-table {
	/* Plain / Stripes: neutral cards that match the desktop table */
	--cof-dt-accent: #1d1d1d;      /* card labels, focus ring: 16:1 on white */
	--cof-dt-border: #1d1d1d;
	--cof-dt-head-bg: #f0f0f0;     /* "First column as card heading" bar */
	--cof-dt-head-fg: #1d1d1d;     /* 14.7:1 on #f0f0f0 */
	--cof-dt-bg: #fff;
	--cof-dt-stripe: #f0f0f0;
	--cof-dt-cell-min: 4.5em;
	container: cof-dt / inline-size; /* only for cqi units (section labels), not breakpoints */
	margin: 0 0 1.5em;
}

/* Columns (and some themes) set word-break: break-word, which splits words mid-way ("Wee k")
   to squeeze columns. Keep words whole; if columns don't fit, the box scrolls instead. */
.wp-block-cof-data-table :is(th, td) {
	word-break: normal;
	overflow-wrap: normal;
	min-width: var(--cof-dt-cell-min);
	/* The theme sets border-box everywhere, so padding (1rem a side on Blue tables) ate into
	   the min width and "Week 1" wrapped. Content-box makes the min width the text area itself.
	   Stacked cards set border-box again with a more specific rule. */
	box-sizing: content-box;
}
/* Header cells: at least --cof-dt-cell-min wide, and (with word-break reset above)
   never narrower than their longest word, so "Week" stays whole */
.wp-block-cof-data-table thead th {
	min-width: var(--cof-dt-cell-min);
	white-space: normal;
	hyphens: manual;
}

.wp-block-cof-data-table .cof-dt__scroll {
	overflow-x: auto;
}

.wp-block-cof-data-table .cof-dt__table {
	width: 100%;
	border-collapse: collapse;
}

/* Blue table style: blue cards to match its blue header */
.wp-block-cof-data-table.is-style-blue-table {
	--cof-dt-accent: #0063a7;      /* 6.1:1 with white */
	--cof-dt-border: #90b6d2;
	--cof-dt-head-bg: #0063a7;
	--cof-dt-head-fg: #fff;
}

/* Zero-specificity fallbacks: the theme's own table styles win wherever they exist
   (Blue comes from the theme's .is-style-blue-table). */
:where(.wp-block-cof-data-table) :where(th, td) {
	border: 1px solid;
	padding: .5em;
}
.wp-block-cof-data-table.is-style-stripes tbody tr:nth-child(odd) {
	background: var(--cof-dt-stripe);
}

.wp-block-cof-data-table .cof-dt__caption {
	caption-side: top;
	text-align: left;
	font-weight: 700;
	margin-bottom: .5em;
}

/* Stack mode turns the table into display:block, which would shrink a visible caption
   to one letter per line. Keep it full width. */
.wp-block-cof-data-table.cof-dt--stack .cof-dt__caption {
	display: block;
	width: auto;
}

/* Row headings (first column) keep the look of normal cells; the change is semantic only */
.wp-block-cof-data-table tbody th[scope="row"] {
	font-weight: inherit;
	text-align: inherit;
}

/* Section heading rows ("Meter Charges, $/month – Domestic") */
.wp-block-cof-data-table .cof-dt__section th {
	text-align: left;
	font-weight: 700;
}
.wp-block-cof-data-table .cof-dt__section-label {
	position: sticky; /* stays in view while a wide table scrolls sideways */
	left: .5em;
}

/* Screen-reader-only text: hidden captions, repeated first-column values */
.wp-block-cof-data-table .cof-dt__sr {
	position: absolute !important;
	width: 1px !important;
	height: 1px !important;
	padding: 0 !important;
	margin: -1px !important;
	overflow: hidden !important;
	clip: rect(0 0 0 0) !important;
	white-space: nowrap !important;
	border: 0 !important;
}

/* Keyboard focus on a sideways-scrolling table (WCAG 2.4.7) */
.wp-block-cof-data-table .cof-dt__scroll:focus-visible {
	outline: 3px solid var(--cof-dt-accent);
	outline-offset: 2px;
}

/* --------------------------------------------------------------------------
   Breakpoint sm: screen 600px and below
   -------------------------------------------------------------------------- */
@media (max-width: 600px) {

	/* Stack: each row becomes a card */
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack .cof-dt__table,
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody,
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tr,
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack th,
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack td {
		display: block;
		width: 100%;
		box-sizing: border-box;
	}

	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack thead {
		position: absolute !important;
		width: 1px !important;
		height: 1px !important;
		padding: 0 !important;
		margin: -1px !important;
		overflow: hidden !important;
		clip: rect(0 0 0 0) !important;
		white-space: nowrap !important;
		border: 0 !important;
	}

	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody tr {
		margin: 0 0 12px;
		border: 1px solid var(--cof-dt-border);
		border-radius: 6px;
		overflow: hidden;
		background: var(--cof-dt-bg);
	}

	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody > tr > th,
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody > tr > td {
		position: relative;
		border: 0 !important;
		border-bottom: 1px solid var(--cof-dt-border) !important;
		padding: 10px 14px !important;
		text-align: left !important;
		background: none;
		color: inherit;
		overflow-wrap: break-word; /* cards are one column wide: a too-long word may break here */
	}

	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody > tr > :last-child {
		border-bottom: 0 !important;
	}

	/* Label from the header row; em-based so it grows with text zoom. Floated (not absolute)
	   inside a flow-root cell so a long, wrapping label makes the row taller instead of
	   overlapping the next one (WCAG 1.4.4 / 1.4.12). The `/ ""` alt text keeps screen
	   readers from reading the label twice — they already get it from the column header. */
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack [data-label] {
		display: flow-root;
		padding-left: calc(6.5em + 14px) !important;
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack [data-label]::before {
		content: attr(data-label);
		content: attr(data-label) / "";
		float: left;
		width: 6em;
		margin-left: -6.5em;
		overflow-wrap: anywhere;
		font-weight: 700;
		color: var(--cof-dt-accent);
	}

	/* Section heading rows sit above their cards */
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody tr.cof-dt__section {
		margin: 24px 0 8px;
		border: 0;
		border-radius: 0;
		background: none;
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody tr.cof-dt__section > th {
		padding: 0 !important;
		border: 0 !important;
		font-size: 1.1em;
	}

	/* First column as a colored bar at the top of each card */
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--card-heading tbody > tr:not(.cof-dt__section) > :first-child {
		padding-left: 14px !important;
		background: var(--cof-dt-head-bg);
		color: var(--cof-dt-head-fg);
		font-weight: 700;
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--card-heading tbody > tr:not(.cof-dt__section) > :first-child::before {
		content: none;
	}

	/* Repeated or empty first cells: hidden visually, still in the table for screen readers */
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody > tr > .cof-dt__repeat,
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--stack tbody > tr > td:empty {
		position: absolute !important;
		width: 1px !important;
		height: 1px !important;
		padding: 0 !important;
		margin: -1px !important;
		overflow: hidden !important;
		clip: rect(0 0 0 0) !important;
		white-space: nowrap !important;
		border: 0 !important;
	}

	/* Scroll: table keeps its columns and scrolls sideways inside its box */
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll .cof-dt__table {
		width: max-content;
		min-width: 100%;
		font-size: .875em;
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll th,
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll td {
		white-space: nowrap;
		word-break: normal;
		overflow-wrap: normal;
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll tr.cof-dt__section > th {
		white-space: normal;
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll .cof-dt__section-label {
		display: inline-block;
		max-width: calc(100cqi - 1.5em);
	}

	/* Pinned first column */
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll.cof-dt--pin tr:not(.cof-dt__section) > :first-child {
		position: sticky;
		left: 0;
		z-index: 1;
		background: var(--cof-dt-bg);
		white-space: normal;
		min-width: 8em;
		max-width: 12em;
		overflow-wrap: anywhere;
		box-shadow: 2px 0 0 rgba(0, 0, 0, .15);
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll.cof-dt--pin.is-style-blue-table thead tr > :first-child {
		background: var(--cof-dt-accent);
	}
	.wp-block-cof-data-table.cof-dt--bp-sm.cof-dt--scroll.cof-dt--pin.is-style-stripes tbody tr:nth-child(odd) > :first-child {
		background: var(--cof-dt-stripe);
	}
}

/* --------------------------------------------------------------------------
   Breakpoint md: screen 767px and below
   -------------------------------------------------------------------------- */
@media (max-width: 767px) {

	/* Stack: each row becomes a card */
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack .cof-dt__table,
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody,
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tr,
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack th,
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack td {
		display: block;
		width: 100%;
		box-sizing: border-box;
	}

	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack thead {
		position: absolute !important;
		width: 1px !important;
		height: 1px !important;
		padding: 0 !important;
		margin: -1px !important;
		overflow: hidden !important;
		clip: rect(0 0 0 0) !important;
		white-space: nowrap !important;
		border: 0 !important;
	}

	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody tr {
		margin: 0 0 12px;
		border: 1px solid var(--cof-dt-border);
		border-radius: 6px;
		overflow: hidden;
		background: var(--cof-dt-bg);
	}

	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody > tr > th,
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody > tr > td {
		position: relative;
		border: 0 !important;
		border-bottom: 1px solid var(--cof-dt-border) !important;
		padding: 10px 14px !important;
		text-align: left !important;
		background: none;
		color: inherit;
		overflow-wrap: break-word; /* cards are one column wide: a too-long word may break here */
	}

	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody > tr > :last-child {
		border-bottom: 0 !important;
	}

	/* Label from the header row; em-based so it grows with text zoom. Floated (not absolute)
	   inside a flow-root cell so a long, wrapping label makes the row taller instead of
	   overlapping the next one (WCAG 1.4.4 / 1.4.12). The `/ ""` alt text keeps screen
	   readers from reading the label twice — they already get it from the column header. */
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack [data-label] {
		display: flow-root;
		padding-left: calc(6.5em + 14px) !important;
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack [data-label]::before {
		content: attr(data-label);
		content: attr(data-label) / "";
		float: left;
		width: 6em;
		margin-left: -6.5em;
		overflow-wrap: anywhere;
		font-weight: 700;
		color: var(--cof-dt-accent);
	}

	/* Section heading rows sit above their cards */
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody tr.cof-dt__section {
		margin: 24px 0 8px;
		border: 0;
		border-radius: 0;
		background: none;
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody tr.cof-dt__section > th {
		padding: 0 !important;
		border: 0 !important;
		font-size: 1.1em;
	}

	/* First column as a colored bar at the top of each card */
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--card-heading tbody > tr:not(.cof-dt__section) > :first-child {
		padding-left: 14px !important;
		background: var(--cof-dt-head-bg);
		color: var(--cof-dt-head-fg);
		font-weight: 700;
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--card-heading tbody > tr:not(.cof-dt__section) > :first-child::before {
		content: none;
	}

	/* Repeated or empty first cells: hidden visually, still in the table for screen readers */
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody > tr > .cof-dt__repeat,
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--stack tbody > tr > td:empty {
		position: absolute !important;
		width: 1px !important;
		height: 1px !important;
		padding: 0 !important;
		margin: -1px !important;
		overflow: hidden !important;
		clip: rect(0 0 0 0) !important;
		white-space: nowrap !important;
		border: 0 !important;
	}

	/* Scroll: table keeps its columns and scrolls sideways inside its box */
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll .cof-dt__table {
		width: max-content;
		min-width: 100%;
		font-size: .875em;
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll th,
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll td {
		white-space: nowrap;
		word-break: normal;
		overflow-wrap: normal;
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll tr.cof-dt__section > th {
		white-space: normal;
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll .cof-dt__section-label {
		display: inline-block;
		max-width: calc(100cqi - 1.5em);
	}

	/* Pinned first column */
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll.cof-dt--pin tr:not(.cof-dt__section) > :first-child {
		position: sticky;
		left: 0;
		z-index: 1;
		background: var(--cof-dt-bg);
		white-space: normal;
		min-width: 8em;
		max-width: 12em;
		overflow-wrap: anywhere;
		box-shadow: 2px 0 0 rgba(0, 0, 0, .15);
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll.cof-dt--pin.is-style-blue-table thead tr > :first-child {
		background: var(--cof-dt-accent);
	}
	.wp-block-cof-data-table.cof-dt--bp-md.cof-dt--scroll.cof-dt--pin.is-style-stripes tbody tr:nth-child(odd) > :first-child {
		background: var(--cof-dt-stripe);
	}
}

/* --------------------------------------------------------------------------
   Breakpoint lg: screen 1024px and below
   -------------------------------------------------------------------------- */
@media (max-width: 1024px) {

	/* Stack: each row becomes a card */
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack .cof-dt__table,
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody,
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tr,
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack th,
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack td {
		display: block;
		width: 100%;
		box-sizing: border-box;
	}

	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack thead {
		position: absolute !important;
		width: 1px !important;
		height: 1px !important;
		padding: 0 !important;
		margin: -1px !important;
		overflow: hidden !important;
		clip: rect(0 0 0 0) !important;
		white-space: nowrap !important;
		border: 0 !important;
	}

	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody tr {
		margin: 0 0 12px;
		border: 1px solid var(--cof-dt-border);
		border-radius: 6px;
		overflow: hidden;
		background: var(--cof-dt-bg);
	}

	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody > tr > th,
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody > tr > td {
		position: relative;
		border: 0 !important;
		border-bottom: 1px solid var(--cof-dt-border) !important;
		padding: 10px 14px !important;
		text-align: left !important;
		background: none;
		color: inherit;
		overflow-wrap: break-word; /* cards are one column wide: a too-long word may break here */
	}

	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody > tr > :last-child {
		border-bottom: 0 !important;
	}

	/* Label from the header row; em-based so it grows with text zoom. Floated (not absolute)
	   inside a flow-root cell so a long, wrapping label makes the row taller instead of
	   overlapping the next one (WCAG 1.4.4 / 1.4.12). The `/ ""` alt text keeps screen
	   readers from reading the label twice — they already get it from the column header. */
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack [data-label] {
		display: flow-root;
		padding-left: calc(6.5em + 14px) !important;
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack [data-label]::before {
		content: attr(data-label);
		content: attr(data-label) / "";
		float: left;
		width: 6em;
		margin-left: -6.5em;
		overflow-wrap: anywhere;
		font-weight: 700;
		color: var(--cof-dt-accent);
	}

	/* Section heading rows sit above their cards */
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody tr.cof-dt__section {
		margin: 24px 0 8px;
		border: 0;
		border-radius: 0;
		background: none;
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody tr.cof-dt__section > th {
		padding: 0 !important;
		border: 0 !important;
		font-size: 1.1em;
	}

	/* First column as a colored bar at the top of each card */
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--card-heading tbody > tr:not(.cof-dt__section) > :first-child {
		padding-left: 14px !important;
		background: var(--cof-dt-head-bg);
		color: var(--cof-dt-head-fg);
		font-weight: 700;
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--card-heading tbody > tr:not(.cof-dt__section) > :first-child::before {
		content: none;
	}

	/* Repeated or empty first cells: hidden visually, still in the table for screen readers */
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody > tr > .cof-dt__repeat,
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--stack tbody > tr > td:empty {
		position: absolute !important;
		width: 1px !important;
		height: 1px !important;
		padding: 0 !important;
		margin: -1px !important;
		overflow: hidden !important;
		clip: rect(0 0 0 0) !important;
		white-space: nowrap !important;
		border: 0 !important;
	}

	/* Scroll: table keeps its columns and scrolls sideways inside its box */
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll .cof-dt__table {
		width: max-content;
		min-width: 100%;
		font-size: .875em;
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll th,
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll td {
		white-space: nowrap;
		word-break: normal;
		overflow-wrap: normal;
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll tr.cof-dt__section > th {
		white-space: normal;
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll .cof-dt__section-label {
		display: inline-block;
		max-width: calc(100cqi - 1.5em);
	}

	/* Pinned first column */
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll.cof-dt--pin tr:not(.cof-dt__section) > :first-child {
		position: sticky;
		left: 0;
		z-index: 1;
		background: var(--cof-dt-bg);
		white-space: normal;
		min-width: 8em;
		max-width: 12em;
		overflow-wrap: anywhere;
		box-shadow: 2px 0 0 rgba(0, 0, 0, .15);
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll.cof-dt--pin.is-style-blue-table thead tr > :first-child {
		background: var(--cof-dt-accent);
	}
	.wp-block-cof-data-table.cof-dt--bp-lg.cof-dt--scroll.cof-dt--pin.is-style-stripes tbody tr:nth-child(odd) > :first-child {
		background: var(--cof-dt-stripe);
	}
}

/* --------------------------------------------------------------------------
   Windows High Contrast / forced colors
   -------------------------------------------------------------------------- */
@media (forced-colors: active) {
	.wp-block-cof-data-table .cof-dt__scroll:focus-visible {
		outline-color: Highlight;
	}
}
