/* ==========================================================================
   DS Chapter List — CoachOS Design System
   Chapter Cards primitive (Direction F). Each group is a tall card with a
   leading progress ring + tinted header gradient + generous body. Items
   breathe inside with hover-reveal action buttons.

   Variants:
     • .ds-chapter-list             — interactive list. Used by CHECKLIST and
                                      PREDEFINED_CHECKLIST answering.
     • .ds-chapter-list--readonly   — aggregation modifier. Items show
                                      participant count badges; the progress
                                      ring shows aggregate completion. Reuses
                                      every structural rule of .ds-chapter-list.

   Card name input behavior:
     • Editable groups (free-form / custom) use a borderless text input.
     • Locked default groups (PREDEFINED_CHECKLIST seeds) add `[readonly]`
       to the same input. When readonly, no underline / no focus outline.

   Accessibility (consumer responsibility):
     • Each item's toggle (.ds-chapter-list__item-check input) must wire
       aria-labelledby to the sibling .ds-chapter-list__item-name input's
       id so screen readers announce the item text. The primitive doesn't
       generate IDs — consumers must.
     • Readonly variant items are non-interactive; ensure they render as
       static text, not buttons.

   Selection / completion is communicated via accent fill on the check mark
   plus a strikethrough on the item name — never via cell borders.

   Consumer contract:
     • In the interactive variant, .ds-chapter-list__name and
       .ds-chapter-list__item-name MUST be <input type="text"> for the
       focus-bottom-border + [readonly] lockdown to work. In the
       --readonly variant they may be <div> (typography rules still apply).
     • Each item-toggle's aria-labelledby should reference the sibling
       item-name input's id. Note: VoiceOver iOS has historically been
       inconsistent for editable input values used as labels; if an
       accessibility audit flags issues, fall back to a hidden <span>
       mirror of the item name.
     • Hover-reveal action buttons (__menu-btn, __item-actions) are
       intentionally tab-stoppable while invisible (opacity 0) so keyboard
       users can reach them. Do NOT swap to visibility:hidden or display:none
       — that removes them from tab order. The pattern relies on
       :focus-within to make them visible during keyboard navigation.
     • Clicking on __item-name text input must NOT toggle the item's
       checkbox. The <label> wraps both the hidden checkbox and the text
       input; the text input absorbs direct clicks per HTML5 spec, but
       consumer Stimulus controllers must NOT add click handlers on
       .ds-chapter-list__item directly — bind to the hidden checkbox's
       `change` event instead.
   ========================================================================== */

/* ── Container ─────────────────────────────────────────────────────────── */

.ds-chapter-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

/* Optional wrapper for consumers that need a Stimulus / JS target around
   the cards without a separate target on every card. `display: contents`
   removes the wrapper from layout so its child cards behave as direct
   flex items of `.ds-chapter-list` and inherit the container's gap.
   Used by participations/elements/questions/_checklist_base.html.erb to
   give the checklist-input controller a `groupList` target for inserting
   new groups without breaking the inter-card spacing. */
.ds-chapter-list__group-list {
  display: contents;
}

/* ── Card ──────────────────────────────────────────────────────────────── */

.ds-chapter-list__card {
  background-color: var(--surface-base);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-md);
  /* `overflow: visible` (default) so child popovers (kebab dropdown) can
     escape the card. Corner-clipping is handled by giving .ds-chapter-list__head
     its own top-corner radii (matches the card's top), and the body's padding
     keeps content away from the bottom-corner radii. */
  transition: box-shadow var(--duration-fast) var(--ease-out);
}

.ds-chapter-list__card:hover {
  box-shadow: var(--shadow-lg);
}

/* ── Card head (tinted gradient + progress ring + name + menu) ────────── */

.ds-chapter-list__head {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-5);
  background: linear-gradient(135deg, var(--accent-subtle) 0%, transparent 100%);
  border-bottom: 1px solid var(--border-subtle);
  /* Carry the card's top-corner radii so the gradient doesn't extend past
     the rounded card edges (the card itself uses overflow: visible to allow
     popover children to escape). */
  border-top-left-radius: var(--radius-xl);
  border-top-right-radius: var(--radius-xl);
}

/* ── Progress ring ─────────────────────────────────────────────────────── */

.ds-chapter-list__ring {
  --ds-chapter-ring-pct: 0;
  flex-shrink: 0;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background:
    conic-gradient(
      var(--accent-primary) calc(var(--ds-chapter-ring-pct) * 1%),
      var(--surface-sunken) 0
    );
  display: flex;
  align-items: center;
  justify-content: center;
}

.ds-chapter-list__ring::after {
  content: attr(data-label);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background-color: var(--surface-base);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

/* ── Card head content (name + desc) ───────────────────────────────────── */

.ds-chapter-list__head-content {
  flex: 1;
  min-width: 0;
}

.ds-chapter-list__name {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
  letter-spacing: -0.005em;
  line-height: var(--leading-snug);
  color: var(--text-primary);
  display: block;
  width: 100%;
  border: 0;
  background-color: transparent;
  padding: 0;
  outline: none;
}

.ds-chapter-list__name:focus {
  box-shadow: inset 0 -1px 0 var(--accent-primary);
}

.ds-chapter-list__name[readonly] {
  cursor: default;
}

.ds-chapter-list__name[readonly]:focus {
  box-shadow: none;
  outline: none;
}

.ds-chapter-list__desc {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: var(--leading-snug);
  margin-top: var(--space-1);
}

/* ── Hover-reveal card menu ────────────────────────────────────────────── */

.ds-chapter-list__menu {
  display: flex;
  gap: var(--space-1);
  flex-shrink: 0;
  opacity: 0;
  transition: opacity var(--duration-fast) var(--ease-out);
}

.ds-chapter-list__card:hover .ds-chapter-list__menu,
.ds-chapter-list__card:focus-within .ds-chapter-list__menu {
  opacity: 1;
}

.ds-chapter-list__menu-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  background-color: transparent;
  border: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-tertiary);
  transition:
    background-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
}

.ds-chapter-list__menu-btn:hover {
  background-color: var(--surface-sunken);
  color: var(--text-primary);
}

.ds-chapter-list__menu-btn:focus-visible {
  outline: 2px solid var(--border-focus, var(--accent-primary));
  outline-offset: 2px;
}

/* ── Save modifier ─────────────────────────────────────────────────────────
   Accent-tinted variant for the explicit-save action (Enter / click). Only
   visible while a card is in `new` or `edit` state — see the state-driven
   rules at the bottom of this file. */
.ds-chapter-list__menu-btn--save {
  display: none;
  color: var(--accent-primary);
}
.ds-chapter-list__menu-btn--save:hover {
  background-color: var(--accent-subtle);
  color: var(--accent-primary);
}

/* ── Card body ─────────────────────────────────────────────────────────── */

.ds-chapter-list__body {
  padding: var(--space-3) var(--space-5) var(--space-4);
}

.ds-chapter-list__items {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

/* ── Item ──────────────────────────────────────────────────────────────── */

.ds-chapter-list__item {
  /* position: relative is REQUIRED — anchors the visually-hidden
     .ds-chapter-list__item-input below. See DS Conventions →
     "Visually-hidden form inputs" at /a/design-system/contribute. */
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-out);
  background-color: transparent;
  border: 0;
  text-align: left;
  width: 100%;
}

.ds-chapter-list__item:hover {
  background-color: var(--surface-hover);
}

.ds-chapter-list__item-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.ds-chapter-list__item-check {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1.5px var(--border-strong);
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition:
    box-shadow var(--duration-fast) var(--ease-out),
    background-color var(--duration-fast) var(--ease-out);
  position: relative;
}

.ds-chapter-list__item-check::after {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--text-on-accent);
  opacity: 0;
  transition: opacity var(--duration-fast) var(--ease-out);
}

.ds-chapter-list__item--on .ds-chapter-list__item-check,
.ds-chapter-list__item:has(.ds-chapter-list__item-input:checked) .ds-chapter-list__item-check {
  background-color: var(--accent-primary);
  box-shadow: inset 0 0 0 1.5px var(--accent-primary);
}

.ds-chapter-list__item--on .ds-chapter-list__item-check::after,
.ds-chapter-list__item:has(.ds-chapter-list__item-input:checked) .ds-chapter-list__item-check::after {
  opacity: 1;
}

.ds-chapter-list__item:has(.ds-chapter-list__item-input:focus-visible) .ds-chapter-list__item-check {
  outline: 2px solid var(--border-focus, var(--accent-primary));
  outline-offset: 2px;
}

/* ── Item name (editable input in interactive variant; <div> in readonly) ── */

.ds-chapter-list__item-name {
  flex: 1;
  min-width: 0;
  font-family: var(--font-display);
  font-size: var(--text-base);
  color: var(--text-primary);
  line-height: var(--leading-snug);
  background-color: transparent;
  border: 0;
  padding: 0;
  outline: none;
  width: 100%;
}

/* Input-only state rules (no-ops on <div> consumers in --readonly). */
.ds-chapter-list__item-name:focus {
  box-shadow: inset 0 -1px 0 var(--accent-primary);
}

.ds-chapter-list__item--on .ds-chapter-list__item-name,
.ds-chapter-list__item:has(.ds-chapter-list__item-input:checked) .ds-chapter-list__item-name {
  text-decoration: line-through;
  text-decoration-color: var(--text-disabled);
  color: var(--text-tertiary);
}

/* ── Item meta (timestamp) ─────────────────────────────────────────────── */

.ds-chapter-list__item-meta {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

/* ── Hover-reveal item action button ───────────────────────────────────── */

.ds-chapter-list__item-actions {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-md);
  background-color: transparent;
  border: 0;
  cursor: pointer;
  color: var(--text-tertiary);
  opacity: 0;
  transition:
    opacity var(--duration-fast) var(--ease-out),
    background-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
  flex-shrink: 0;
}

.ds-chapter-list__item:hover .ds-chapter-list__item-actions,
.ds-chapter-list__item:focus-within .ds-chapter-list__item-actions {
  opacity: 1;
}

.ds-chapter-list__item-actions:hover {
  background-color: var(--surface-sunken);
  color: var(--text-primary);
}

/* ── Add buttons ───────────────────────────────────────────────────────── */

.ds-chapter-list__card-add {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  margin-top: var(--space-2);
  background-color: transparent;
  border: 1px dashed var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-tertiary);
  font-family: inherit;
  font-size: var(--text-sm);
  cursor: pointer;
  transition:
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
  align-self: flex-start;
}

.ds-chapter-list__card-add:hover {
  border-color: var(--accent-muted);
  color: var(--accent-primary);
}

.ds-chapter-list__add {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  justify-content: center;
  padding: var(--space-3) var(--space-4);
  background-color: var(--surface-base);
  border: 1px dashed var(--border-default);
  border-radius: var(--radius-lg);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  cursor: pointer;
  transition:
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
  align-self: stretch;
}

.ds-chapter-list__add:hover {
  border-color: var(--accent-muted);
  color: var(--accent-primary);
}

/* ── Item count badge (readonly variant only) ─────────────────────────── */

.ds-chapter-list__item-count {
  flex-shrink: 0;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  padding: var(--space-0-5) var(--space-2);
  border-radius: var(--radius-full);
  background-color: var(--surface-sunken);
}

/* ==========================================================================
   .ds-chapter-list--readonly — aggregation modifier
   Reuses the structural chrome above. Cards lose hover lift; items lose
   hover/cursor; the menu is hidden. Item names render as <div> so the
   input-only :focus / [readonly] rules naturally don't apply.
   ========================================================================== */

.ds-chapter-list--readonly .ds-chapter-list__card:hover {
  box-shadow: var(--shadow-md);
}

.ds-chapter-list--readonly .ds-chapter-list__menu {
  display: none;
}

.ds-chapter-list--readonly .ds-chapter-list__item {
  cursor: default;
}

.ds-chapter-list--readonly .ds-chapter-list__item:hover {
  background-color: transparent;
}

/* ==========================================================================
   State-driven UI (consumer state machine)
   ──────────────────────────────────────────────────────────────────────────
   Cards may carry a data-group-state attribute to drive new / edit / show /
   locked affordances. The save button (--save) renders only while the card
   is in `new` or `edit` state. Other state-driven rules (kebab visibility,
   static-vs-input name display) are added as those affordances ship.

   Used by Q-Web checklist (CHECKLIST and PREDEFINED_CHECKLIST) via
   participations/elements/questions/_checklist_base.html.erb.
   ========================================================================== */

.ds-chapter-list__card[data-group-state="new"] .ds-chapter-list__menu-btn--save,
.ds-chapter-list__card[data-group-state="edit"] .ds-chapter-list__menu-btn--save {
  display: inline-flex;
}
