/* Data tables: catalog, offers, nutrition, compare. */

@layer components {
:is(.catalog, .nutrition, .compare) {
  border-collapse: collapse; width: 100%; font-size: 0.95rem;

  & caption { text-align: left; color: var(--muted); font-size: 0.85rem; padding-bottom: var(--space-2); }
  & thead th {
    text-align: left; vertical-align: bottom; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em;
    color: var(--muted); border-bottom: 2px solid var(--line-strong); padding: var(--space-2) var(--space-3);
  }
  & :is(td, tbody th) { text-align: left; vertical-align: top; padding: var(--space-3); border-bottom: 1px solid var(--line); font-weight: 400; }
}

:is(.catalog, .nutrition) tbody tr {
  &:hover { background-color: var(--hover); }
  /* An orange line on the row's left edge that grows from the middle on hover
     (motion.css, D-042). */
  & > :first-child { background: linear-gradient(var(--orange), var(--orange)) no-repeat left center / 4px 0%; }
  &:hover > :first-child { background-size: 4px 100%; }
}

/* A product photo beside the name, never wrapping under it; the brand on its own
   small line, so long names break between words of the name only. */
.product-cell { display: flex; align-items: center; gap: var(--space-3); min-width: 13rem; }
.product-cell .product-image--small { margin: 0; }
.product-cell__brand { display: block; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.product-cell__name { display: block; line-height: 1.3; }
.catalog td {
  & > span { white-space: nowrap; } /* "≈256–259 mg" never breaks at the dash */
  & small { display: block; font-size: 0.78rem; color: var(--muted); }
  & .fact-missing { color: var(--muted); }
}

.catalog {
  /* Middle, so values line up with a name centred on its photo tile. */
  & tbody :is(td, th) { vertical-align: middle; }
  & tbody th a { color: var(--text); font-weight: 600; text-decoration: none; }
  & tbody th a:hover { color: var(--link); text-decoration: underline; }
  & + button { margin-top: var(--space-4); }

  /* Narrow screens: rows become labelled blocks (IA §5.2). */
  @media (max-width: 48rem) {
    & thead { position: absolute; left: -999rem; }
    & tr { display: block; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: var(--space-3); padding: var(--space-2) var(--space-3); }
    & :is(td, tbody th) { display: flex; justify-content: space-between; gap: var(--space-4); border: 0; padding: var(--space-1) 0; }
    & td::before { content: attr(data-label); font-weight: 600; color: var(--muted); }
  }
}

.compare {
  /* Equal product columns that fit the page on wide screens; the label column fixed. */
  width: 100%; table-layout: fixed;
  & :is(th, td) { padding: var(--space-2) var(--space-3); line-height: 1.35; overflow-wrap: break-word; }
  & tbody :is(th, td) { vertical-align: middle; }
  & thead :is(th, td) { vertical-align: top; padding-bottom: var(--space-3); }
  & thead td { width: 13rem; }
  & thead th { text-transform: none; letter-spacing: 0; color: var(--text); font-size: 0.95rem; }
  & thead .product-cell { min-width: 0; align-items: flex-start; }
  & thead .product-cell a { color: var(--text); text-decoration: none; &:hover { text-decoration: underline; } }
  /* Row headers stay visible while scrolling sideways (IA §5.6). */
  & tbody th { position: sticky; left: 0; background: var(--bg); font-weight: 600; font-size: 0.92rem; }
  /* Light and grey rows in turn, so a value is easy to follow across. */
  & tbody tr:nth-child(even) > * { background-color: var(--surface); }

  @media (max-width: 56rem) { width: auto; min-width: 100%; & :is(th, td) { min-width: 9rem; } & thead td { width: 9rem; } }
}
.compare-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-1); }
.compare-remove {
  flex: none; display: inline-grid; place-items: center; width: 1.75rem; height: 1.75rem;
  border-radius: 999px; color: var(--muted); text-decoration: none; font-size: 1.2rem; line-height: 1;
  &:hover { background: var(--hover); color: var(--accent-text); }
}

/* "Compare selected": sticks to the bottom of the screen once a product is ticked,
   with how many are ticked (a CSS counter: no script). */
.catalog-compare { counter-reset: picked; }
.catalog-compare input[name="p"]:checked { counter-increment: picked; }
.compare-bar {
  display: none; position: sticky; bottom: var(--space-3); z-index: 6; margin-top: var(--space-3);
  align-items: center; justify-content: space-between; gap: var(--space-3);
  padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
  border-radius: var(--radius); background: var(--navy); color: #fff; box-shadow: 0 8px 24px rgb(20 33 61 / 0.25);
}
.compare-bar__count::after { content: counter(picked) " / 4"; font-weight: 700; }
form:has(input[name="p"]:checked) .compare-bar { display: flex; }

/* An empty comparison page: products to pick, by category. */
.picker-group { border: 0; padding: 0; margin: 0 0 var(--space-5);
  & legend { font-weight: 800; text-transform: uppercase; font-stretch: 112%; font-size: 1rem; padding: 0; margin-bottom: var(--space-2); } }
.picker {
  list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-2);
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
}
.picker__item {
  display: flex; align-items: center; gap: var(--space-2); padding: var(--space-2) var(--space-3);
  border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer; font-size: 0.92rem;
  transition: border-color 0.15s ease, background-color 0.15s ease;
  &:hover { border-color: var(--field-line); }
  &:has(input:checked) { border-color: var(--orange); background: var(--tag-bg); }
  & input { flex: none; accent-color: var(--orange); }
  & .product-image--small { margin: 0; }
}

:is(.nutrition-wrap, .table-scroll) { overflow-x: auto; }

/* htmx: results fade while new ones load. */
:is(.htmx-request#results, .htmx-request #results) { opacity: 0.6; transition: opacity 0.2s; }

/* Nutrition by flavour: numbers right-aligned in tabular figures, one value per cell. */
.table-scroll { overflow-x: auto; &:focus-visible { outline: 2px solid var(--orange); outline-offset: 2px; } }
.nutrition {
  & :is(td, th).num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
  & thead th.num { white-space: normal; min-width: 6rem; }
  & tbody th { font-weight: 600; white-space: nowrap; }
  & tbody td { font-weight: 500; }
}
.table-note { margin: var(--space-2) 0 0; color: var(--muted); }
/* A value that differs by flavour (D-060): a small tag, not a footnote star. */
.varies {
  position: relative; display: inline-flex; vertical-align: 0.55em;
  margin-left: 0.15em; color: var(--muted); line-height: 0; cursor: help; border-radius: 2px;
  & svg { width: 10px; height: 6px; }
  /* Our tip instead of the browser's: navy label, small arrow, above the mark. */
  &::before, &::after {
    position: absolute; left: 50%; bottom: calc(100% + 6px); z-index: 10;
    opacity: 0; pointer-events: none; transform: translate(-50%, 3px);
    transition: opacity 0.15s ease, transform 0.15s ease;
  }
  &::after {
    content: attr(data-tip); padding: 5px 9px; border-radius: var(--radius);
    background: var(--navy); color: #fff; box-shadow: 0 4px 14px rgb(20 33 61 / 0.25);
    font-size: 0.75rem; font-weight: 600; line-height: 1.2; white-space: nowrap;
  }
  &::before {
    content: ""; bottom: calc(100% + 1px); border: 5px solid transparent; border-top-color: var(--navy);
    border-bottom-width: 0;
  }
  &:is(:hover, :focus-visible)::before, &:is(:hover, :focus-visible)::after { opacity: 1; transform: translate(-50%, 0); }
  &:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
}
.varies-note { display: flex; align-items: center; gap: 0.4em; & .varies { margin-left: 0; vertical-align: 0; } }
.label-values {
  margin-top: var(--space-4);
  & summary { cursor: pointer; font-weight: 600; color: var(--link); }
  & .nutrition { margin-top: var(--space-3); font-size: 0.875rem; }
}
}

/* Offers grouped by flavour on the product page: the flavour is the first column,
   spanning its offers. Lines separate flavours only; offers of one flavour sit
   together without lines (proximity groups them). */
@layer components {
.offers .offer-flavour { vertical-align: top; font-weight: 700; color: var(--text); }
@media (min-width: 48.0625rem) {
  .offers {
    & tbody + tbody { border-top: 1px solid var(--line); }
    & tbody tr > * { border-bottom: 0; }
    & tbody tr:first-child > * { padding-top: var(--space-4); }
    & tbody tr:last-child > * { padding-bottom: var(--space-4); }
    & tbody tr:not(:first-child) > * { padding-top: 0; }
    & .offer-flavour { width: 14rem; }
  }
}

/* Narrow screens: one card per flavour, its offers inside, separated by a thin line. */
@media (max-width: 48rem) {
  .offers {
    & tbody { display: block; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: var(--space-3); padding: var(--space-1) var(--space-3); }
    & tbody + tbody { border-top: 1px solid var(--line); }
    & tbody tr { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2) var(--space-4); border: 0; border-radius: 0; margin: 0; padding: var(--space-2) 0; }
    & tbody tr + tr { border-top: 1px dashed var(--line); }
    & tbody tr > * { padding: 0; }
    & :is(th[scope="row"], .offer-flavour) { grid-column: 1 / -1; width: auto; }
    & .offer-flavour { font-size: 1rem; padding-bottom: var(--space-1); }
    & tbody td { flex-direction: column; justify-content: flex-start; gap: 0.1rem; }
    & td::before { font-size: 0.75rem; font-weight: 600; }
  }
}
}

/* A product's unit size after its name, set apart: names can hold numbers of their own
   ("Fuel Cubes 30 · 40 g"). */
@layer components {
.product-size {
  white-space: nowrap; color: var(--muted); font-weight: 500; font-size: 0.9em;
  &::before { content: "· "; }
}
}

/* A plain, dense table for secondary lists ("not in this list"): no cards on phones,
   no hover line, small type. */
@layer components {
.compact-table {
  border-collapse: collapse; width: 100%; max-width: 48rem; margin: var(--space-3) 0 var(--space-5); font-size: 0.9rem;
  & :is(th, td) { text-align: left; padding: 0.35rem var(--space-3) 0.35rem 0; border-bottom: 1px solid var(--line); vertical-align: top; }
  & thead th { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); border-bottom-color: var(--line-strong); }
  & tbody th { font-weight: 500; }
  & td:first-child { color: var(--muted); white-space: nowrap; }
  & td:last-child { color: var(--muted); }
}
}

/* A flavour with declared caffeine (review, 2026-12-04). */
@layer components {
.badge { display: inline-block; padding: 0.05rem 0.45rem; border-radius: 999px; font-size: 0.7rem; font-weight: 700; vertical-align: 0.1em; white-space: nowrap; }
.badge--caffeine { background: #3b2a1e; color: #fff; }
}
