/* ==========================================================================
   Rating — stars you can pick, built on native radio buttons (no JavaScript
   needed; arrow keys, forms and validation work like any radio group).

   <fieldset class="rating">
     <legend class="visually-hidden">Rate this product</legend>
     <input type="radio" name="stars" value="1" aria-label="1 star">
     <input type="radio" name="stars" value="2" aria-label="2 stars">
     … up to 5 (or 10) …
   </fieldset>

   Read-only, with fractions:
   <span class="rating-static" data-value="4.5" role="img" aria-label="4.5 out of 5"></span>   (data-max="10" for 10)

   .rating-sm · .rating-lg     sizes        .rating-heart   hearts instead of stars
   theme-*                     color        data-moey-clearable   click the chosen star again to clear (moey.js)
   ========================================================================== */

.rating,
.rating-static {
  --mo-rating-size: 1.5rem;
  --mo-rating-color: var(--mo-amber-400);
  --mo-rating-empty: color-mix(in oklab, var(--mo-fg-body) 14%, transparent);
  --mo-rating-shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z' stroke='black' stroke-width='1.5' stroke-linejoin='round'/%3E%3C/svg%3E");
}
.rating-heart {
  --mo-rating-color: var(--mo-pink-500);
  --mo-rating-shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M2 9.5a5.5 5.5 0 0 1 9.591-3.676.56.56 0 0 0 .818 0A5.49 5.49 0 0 1 22 9.5c0 2.29-1.5 4-3 5.5l-5.492 5.313a2 2 0 0 1-3 .019L5 15c-1.5-1.5-3-3.2-3-5.5' stroke='black' stroke-width='1.5' stroke-linejoin='round'/%3E%3C/svg%3E");
}
.rating-sm { --mo-rating-size: 1.125rem; }
.rating-lg { --mo-rating-size: 2rem; }
:is(.rating, .rating-static)[class*="theme-"] { --mo-rating-color: var(--mo-theme-strong); }

/* Pickable ------------------------------------------------------------------------ */
.rating {
  display: inline-flex;
  min-width: 0;
  padding: 0;
  margin: 0;
  vertical-align: middle;
  border: 0;
  border-radius: var(--mo-radius-control);
}
.rating > legend { float: left; }               /* a visible legend sits beside the stars */
.rating > input {
  flex-shrink: 0;
  width: var(--mo-rating-size);
  height: var(--mo-rating-size);
  margin: 0;
  cursor: pointer;
  background-color: var(--mo-rating-color);
  border: 0;
  appearance: none;
  mask: var(--mo-rating-shape) center / 88% no-repeat;
}
@media (prefers-reduced-motion: no-preference) {
  .rating > input { transition: background-color .12s, scale .12s; }
}
/* Order matters below: later rules win.
   1. Stars after the chosen one are empty; with nothing chosen, all are empty. */
.rating > input:checked ~ input,
.rating:not(:has(> input:checked)) > input { background-color: var(--mo-rating-empty); }
/* 2. Hover previews: everything up to the star under the pointer fills */
.rating:has(> input:hover:enabled) > input:enabled { background-color: var(--mo-rating-color); }
.rating:has(> input:hover:enabled) > input:hover:enabled ~ input:enabled { background-color: var(--mo-rating-empty); }   /* same :has() as above, so it outranks it */
.rating > input:hover:enabled { scale: 1.12; }

/* The star itself is a masked shape (an outline would be cut off), so the
   keyboard focus ring goes around the whole group, and the focused star grows */
.rating:has(> input:focus-visible) { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: 2px; }
.rating > input:focus-visible { scale: 1.12; }

.rating > input:disabled { cursor: default; }
.rating:has(> input:disabled) { opacity: .6; }
/* Read-only but still sent with the form: <fieldset class="rating" data-readonly> */
.rating[data-readonly] { pointer-events: none; }

/* Read-only display, any fraction ------------------------------------------------- */
.rating-static {
  --mo-rating-max: 5;
  display: inline-block;
  width: calc(var(--mo-rating-size) * var(--mo-rating-max));
  height: var(--mo-rating-size);
  vertical-align: middle;
  background: linear-gradient(to right,
    var(--mo-rating-color) calc(var(--mo-rating-value, 0) / var(--mo-rating-max) * 100%),
    var(--mo-rating-empty) 0);
  mask: var(--mo-rating-shape) left center / var(--mo-rating-size) 88% repeat-x;
}
.rating-static:dir(rtl) { scale: -1 1; }

/* data-value="4.5" (and data-max) → the stars: read from the attribute where CSS can (typed attr()), by moey.js elsewhere */
@supports (x: attr(x type(*))) {
  .rating-static[data-value] { --mo-rating-value: attr(data-value type(<number>), 0); }
  .rating-static[data-max] { --mo-rating-max: attr(data-max type(<number>), 5); }
}
