/**
 * Mirrors the table styling in frost-trunk's style.css (the `.wp-block-table,
 * .wp-block-flexible-table-block-table` rules) so this block sits alongside
 * the existing tables without a restyle.  Everything resolves through preset
 * custom properties, so a palette change in theme.json carries over.
 */

.wp-block-aakh-table {
	--atb-frame-width: 1px;
	--atb-frame-radius: var( --border-radius-default, 8px );

	/* A rounded frame's *inner* curve is tighter than its outer one by the
	   border width. Cells clipping to the outer radius cut away more than
	   the frame does, leaving a crescent of figure background showing at
	   each corner. */
	--atb-cell-radius: calc( var( --atb-frame-radius ) - var( --atb-frame-width ) );

	/* The grid follows the text colour rather than a pinned preset, so the
	   table works wherever it is dropped. On an ordinary page currentColor is
	   already `primary` — the site's body text — so this draws exactly what
	   the pinned preset used to; inside a group set to white text it inverts
	   along with everything else instead of ruling navy on navy. */
	--atb-rule: currentColor;

	/* Stripe fill, overridden per header colour further down. The default is
	   the original fixed treatment: sky at 50%, which lands on ~#E9F5F7. */
	--atb-stripe: color-mix( in srgb, var( --wp--preset--color--sky ) 50%, transparent );

	/* Header fill, and the ink that has to read against it — text and the
	   dividers between filled cells both. Overridden per header colour by the
	   generated rules; these defaults describe the untouched header. */
	--atb-header-bg:  var( --wp--preset--color--primary );
	--atb-header-ink: var( --wp--preset--color--base );

	margin: 0 0 1.5em;
	border: var( --atb-frame-width ) solid var( --atb-rule );
	border-radius: var( --atb-frame-radius );
	font-size: var( --wp--preset--font-size--small );
	overflow-x: auto;

	/* No background of its own. An opaque white fill turned the table into a
	   white card whenever it sat in a coloured section; the Background control
	   paints one when that is actually wanted. */
	background-color: transparent;
}

.wp-block-aakh-table table {
	width: 100%;
	border-collapse: collapse;
}

.wp-block-aakh-table td,
.wp-block-aakh-table th {
	padding: 0.5em;
	border: 1px solid var( --atb-rule );
	vertical-align: top;
	text-align: left;
}

/* The figure draws the outer edge; cells only draw the grid inside it. */
.wp-block-aakh-table tr > *:first-child { border-left-width: 0; }
.wp-block-aakh-table tr > *:last-child  { border-right-width: 0; }
.wp-block-aakh-table tr:first-child > * { border-top-width: 0; }
.wp-block-aakh-table tr:last-child > *  { border-bottom-width: 0; }

/* Corner radii, so a filled header or first column clips to the frame. */
.wp-block-aakh-table tr:first-child > *:first-child { border-top-left-radius: var( --atb-cell-radius ); }
.wp-block-aakh-table tr:first-child > *:last-child  { border-top-right-radius: var( --atb-cell-radius ); }
.wp-block-aakh-table tr:last-child > *:first-child  { border-bottom-left-radius: var( --atb-cell-radius ); }
.wp-block-aakh-table tr:last-child > *:last-child   { border-bottom-right-radius: var( --atb-cell-radius ); }

/* Default header treatment — the theme's thead rule, keyed off <th> so a
   header column gets it too.  Overridden by the Header cells colour. */
.wp-block-aakh-table th {
	font-weight: 700;
	background-color: var( --atb-header-bg );
	color: var( --atb-header-ink );
}

/* Dividers *inside* a filled run have to read against the fill, while the
   edge where the fill meets the body keeps the table's own border colour.
   Which edge is which depends on the direction of the run, so key off the
   scope the header normalisation already sets: col = header row, row =
   header column, neither = the corner cell, which heads both.
   This is the theme's thead + is-style-filled-first-column pair.
   The table's outer edges are already zero-width, so no last-child guard
   is needed — an invisible border has no colour to get wrong.

   The divider takes the header's ink, so it inverts with the fill: white on
   a dark header, and on a light one the same grid colour the body uses,
   which a white divider would otherwise erase. */
.wp-block-aakh-table th[scope="col"] {
	border-right-color: var( --atb-header-ink );
}

.wp-block-aakh-table th[scope="row"] {
	border-top-color: var( --atb-header-ink );
	border-bottom-color: var( --atb-header-ink );
}

.wp-block-aakh-table th:not([scope]) {
	border-right-color: var( --atb-header-ink );
	border-bottom-color: var( --atb-header-ink );
}

/* Stripes.
 *
 * Same treatment as the theme's is-style-stripes: sky at 50%, with the solid
 * colour first as the fallback for anything without color-mix().
 *
 * Scoped to td so a header row or column keeps its own fill and is never
 * striped over. nth-child counts every row or cell including the headers,
 * which is what the odd/even control is for — rather than trying to guess
 * where the body starts, the author flips it until it looks right.
 *
 * Column stripes are positional, so merged cells shift them; the editor
 * warns when that applies. Row stripes are unaffected by spans.
 */
.wp-block-aakh-table.has-striped-rows:not(.is-striped-even) tr:nth-child(odd) td,
.wp-block-aakh-table.has-striped-rows.is-striped-even tr:nth-child(even) td,
.wp-block-aakh-table.has-striped-columns:not(.is-striped-even) td:nth-child(odd),
.wp-block-aakh-table.has-striped-columns.is-striped-even td:nth-child(even) {
	background-color: var( --atb-stripe );
}

/* Stripe tints per header colour.
 *
 * Chosen values rather than a mix percentage of the header: the palette runs
 * from #0C2A4E to #CEE8F0, and no single percentage reads correctly across
 * that range — a tint that works under primary is invisible under sky.
 *
 * primary is absent on purpose; it keeps the default above. So do base and
 * contrast, which were not specified — a white header cannot tint at all.
 */
.wp-block-aakh-table.has-header-color-secondary { --atb-stripe: #E0F0F6; }
.wp-block-aakh-table.has-header-color-neutral   { --atb-stripe: #F0F2F2; }
.wp-block-aakh-table.has-header-color-light     { --atb-stripe: #F2F2F0; }
.wp-block-aakh-table.has-header-color-yellow    { --atb-stripe: #FFF1D8; }
.wp-block-aakh-table.has-header-color-burgundy  { --atb-stripe: #EDD8DE; }
.wp-block-aakh-table.has-header-color-orange    { --atb-stripe: #F6E2D5; }
.wp-block-aakh-table.has-header-color-red       { --atb-stripe: #F1C5C5; }
.wp-block-aakh-table.has-header-color-aqua      { --atb-stripe: #D7F4F7; }
.wp-block-aakh-table.has-header-color-sky       { --atb-stripe: #E9F5F7; }
.wp-block-aakh-table.has-header-color-cream     { --atb-stripe: #F5F3E9; }
.wp-block-aakh-table.has-header-color-red-shade { --atb-stripe: #D9C5C1; }

/* Once the table carries colours of its own, those tints no longer apply:
 * every one of them was picked to sit on a white page, and a pale blue wash
 * over a navy section reads as a mistake.
 *
 * In that case the stripe stops being a colour and becomes a shift — a little
 * of the text colour laid over whatever is behind, so it darkens on a light
 * ground and lightens on a dark one without knowing which it is on. Equal
 * specificity to the tints above, so this wins by coming after them.
 */
.wp-block-aakh-table.has-background,
.wp-block-aakh-table.has-text-color {
	--atb-stripe: color-mix( in srgb, currentColor 7%, transparent );
}

/* Wrapping inside cells.
 *
 * The theme puts overflow-wrap:anywhere on .entry-content p/li so long words
 * can't overflow prose. In a table that has a second effect: `anywhere` counts
 * toward min-content sizing, so the column is allowed to shrink narrower than
 * its longest word — which is what splits a two-digit number down the middle.
 *
 * `break-word` wraps an over-long word the same way but leaves intrinsic width
 * alone, so a column never gets squeezed below one word. Links keep `anywhere`,
 * since a bare URL is the one case that genuinely has to break or it forces the
 * table wide enough to scroll — the same scoping the theme uses for
 * flexible-table-block.
 *
 * Selectors are qualified by td/th to outrank .entry-content p regardless of
 * which stylesheet the site happens to load last.
 */
.wp-block-aakh-table td p,
.wp-block-aakh-table td li,
.wp-block-aakh-table th p,
.wp-block-aakh-table th li {
	overflow-wrap: break-word;
	word-wrap: break-word;
}

.wp-block-aakh-table td a:not( .wp-element-button ),
.wp-block-aakh-table th a:not( .wp-element-button ) {
	overflow-wrap: anywhere;
	word-wrap: anywhere;
}

/* Blocks inside a cell sit tight. */
.wp-block-aakh-table td > *,
.wp-block-aakh-table th > * {
	margin-top: 0;
	margin-bottom: 0.4em;
}

.wp-block-aakh-table td > *:last-child,
.wp-block-aakh-table th > *:last-child {
	margin-bottom: 0;
}

.wp-block-aakh-table li:last-child {
	margin-bottom: 0;
}
