/* Buttons and action rows. One filled button per screen, at the epicenter,
   filled ink and never shu (HANSHI.md, Buttons).

   ONE BUTTON, RETUNED BY CUSTOM PROPERTIES. Every variant below sets three or
   four --btn-* values and nothing else; no variant restates a declaration. Both
   Basecamp and Fizzy arrived at this independently (`.btn--plain{--btn-background:
   transparent}`, `.btn--negative{--btn-background:var(--color-negative)}`), and
   the reason shows up the third time you add a variant: with plain declarations
   each one has to remember padding, radius, border and both hover states, and
   they drift. Here the base owns the anatomy and a variant owns the palette.

   The base declares none of the values it is retuned by: it reads each one
   with its default as the fallback. So a retune never competes with it — one
   class works from any file, whichever loads first (every file is in the same
   layer, and they load in name order) — and a container can retune every
   button inside it. Overriding a VARIANT still takes weight: the variant does
   declare its values.

   There is no separate icon-button system. HANSHI says "icons first: 26–28px
   circles", which is a button whose padding is 0 and whose radius is 50% — so
   it is .btn--circle, and it is also auto-detected, Fizzy-style, for any button
   that carries an icon and names itself with aria-label. */
@layer components {
  /* Anatomy — shared by every variant, which is the point. The mass is
     deliberately below the handoff's: full-size type over half-block padding
     measured 42px tall and read "massive" (user, 2026-08-11) — so a button
     speaks one size down and ~31px tall, subtle beside the text it acts on.
     Recorded under Deviations in HANSHI.md. The block padding is a fixed rem,
     not a half token: a control's size is its own, never the hosting
     surface's density. */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--inline-space-half);
    padding: var(--btn-padding, 0.25rem var(--inline-space-double));
    font-size: var(--btn-font-size, var(--text-small));
    font-weight: 500;
    /* An <input type="submit"> renders its value itself rather than as a child
       box, so the flex alignment above cannot reach it. */
    text-align: center;
    /* A control's label never wraps; a squeezed row scrolls or reflows around
       the button, not through it. */
    white-space: nowrap;
    color: var(--btn-color, var(--color-canvas));
    background-color: var(--btn-background, var(--color-ink));
    border: var(--border-size) solid var(--btn-border-color, transparent);
    border-radius: var(--btn-radius, var(--border-radius));
    text-decoration: var(--btn-decoration, none);
    /* A link's line (base.css), for the variant that underlines. */
    text-decoration-thickness: 1px;
    text-underline-offset: 0.15em;
    cursor: pointer;

    /* Only where something hovers. A phone keeps :hover on the last button
       tapped until the next tap elsewhere, and the chat's send keeps the focus
       in the line, so after one send the filled disc sat in its hover grey
       while you wrote the next: a live button that read as disabled (user,
       2026-10-09). */
    @media (any-hover: hover) {
      &:hover {
        color: var(--btn-color-hover, var(--btn-color, var(--color-canvas)));
        background-color: var(--btn-background-hover, var(--color-ink-subtle));
      }
    }

    /* A disabled button must not light up under the pointer — :hover still
       fires on one. The move-up and move-down controls on a card are disabled
       at the ends of their column (records/_entry_stages.html.erb), so this is
       a live state rather than a hypothetical. */
    &:disabled {
      --btn-background-hover: var(--btn-background, var(--color-ink));
      --btn-color-hover: var(--btn-color, var(--color-canvas));

      opacity: 0.4;
      cursor: default;
    }

    /* ——— Working ———
       Turbo already says when a form is in flight, so a spinner needs no
       controller, no data attribute and no markup: on submit it disables the
       SUBMITTER and sets aria-busy on the FORM, and undoes both when the
       response lands. Verified in the bundle we serve, not assumed —
       `requestStarted` runs `forms.submitter.beforeSubmit` (the shipped
       default strategy is "disabled") and then marks the form busy. Ported
       from ../chalet, which discovered the pair.

       Scoped to `form[aria-busy]` rather than any busy ancestor, which is the
       one place ours departs from theirs: Turbo also marks
       document.documentElement busy for the length of every Drive visit, so
       the unscoped rule spins every genuinely disabled button on the page
       whenever you navigate — and we have real ones (the move controls at the
       ends of a column). A form is the only busy thing that means "this
       button is working".

       Cost of the whole feature: this rule and the drawing in spinner.css.

       The label goes transparent rather than being hidden as a child, because
       an <input type="submit"> draws its value itself and has no child to
       hide — the same reason the flex alignment above cannot reach it. Full
       opacity, because 0.4 is how a button says "unavailable" and this one is
       saying the opposite. Its width never changes, so no row jumps. */
    form[aria-busy] &:disabled {
      /* The dots themselves are spinner.css, which lists this selector.
         --dot-color is the button's own ink and not currentColor, because
         currentColor is transparent here, by the line below. */
      --dot-color: var(--btn-color, var(--color-canvas));
      --dot-inset: 50%;
      --dot-speed: 1s;

      position: relative;
      opacity: 1;
      color: transparent;
    }
  }

  /* Secondary actions: a quiet word with no box (owner, 2026-11-03 — the
     bordered label read as a massive button beside the sentence it acts
     on). Ink-subtle at rest; the pointer brings the wash and full ink, as it
     does for an icon circle, so a word and a glyph answer the pointer alike.
     The 1ch of padding is the wash's own room. */
  .btn--quiet {
    --btn-background: transparent;
    --btn-color: var(--color-ink-subtle);
    --btn-color-hover: var(--color-ink);
    --btn-background-hover: var(--color-surface);
    --btn-padding: 0.25rem var(--inline-space);
  }

  /* A row of quiet levers lines its words up with the column, not its
     washes: the 1ch each wash needs hangs into the gutter, so a strip that
     wraps on a phone starts its levers under the sentence's first letter and
     ends them on the hairline's last pixel. */
  .levers {
    margin-inline: calc(var(--inline-space) * -1);
  }

  /* Bordered in one place only: an action row, beside the submit it is the
     second of ("Never mind" by Save). There it must still read as a control
     of the same weight, and the frame is what says so. :where() keeps this
     at a variant's weight, so a circle below still wins on its own disc. */
  .actions :where(.btn--quiet) {
    --btn-color: var(--color-ink);
    --btn-border-color: var(--color-line);
    --btn-padding: 0.25rem var(--inline-space-double);
  }
  /* A verb that reads as a line of prose rather than a control — "remove my
     picture and show my initials instead", sitting under the picture it talks
     about. Fizzy's .btn--plain, and the reason it exists here: a bordered box
     around a sentence that long is a button the size of the section it belongs
     to (user, 2026-08-13). It is still a button, so it is still .btn; all it
     gives up is the box.

     The card board's column expander wears it too, and needs the ink to be
     inherited rather than quieted — it sits on the column's own gradient. That
     retune lives with the column, in card-columns.css. */
  .btn--plain {
    --btn-background: transparent;
    --btn-color: var(--color-ink-subtle);
    --btn-color-hover: var(--color-ink);
    --btn-background-hover: transparent;
    --btn-padding: 0;
  }

  /* A plain verb inside a line of words, in ink and underlined as a link is
     (base.css), where a box would be a second filled thing or a frame in a
     sentence: an answered invitation's Change (invitation-cards.css),
     Subscribe…'s Reset link. Worn with .btn--plain, after which it sits. */
  .btn--underlined {
    --btn-color: var(--color-ink);
    --btn-decoration: underline;
  }

  /* The link colour is ink, so this is a filled ink button whose hover does
     not change. It exists for the board's Add a card. Everywhere else,
     HANSHI's one-filled-button-per-screen rule still means `.btn`. */
  .btn--link {
    --btn-background: var(--color-link);
    --btn-color: var(--color-canvas);
    --btn-border-color: var(--color-canvas);
    --btn-background-hover: var(--color-link);
  }

  /* Icons first: a circle at the top of the spec's 26–28px range — 1.75rem,
     unless something sets --btn-size — ink-subtle at
     rest, hover = the surface wash plus full ink. Worn by <a> and <label> as
     well as <button>, so it has to kill the underline a link carries.

     The second selector is Fizzy's auto-detection: a button that is nothing but
     an icon has to name itself with aria-label anyway, so the two together are
     already an unambiguous description of a circle — no third class to forget.
     The assignees strip's + trigger (recordings/_assignees) rides it. A plain
     button never becomes one: a filter's × is its glyph alone, and a disc
     around it says nothing the × does not (owner, 2026-11-03). */
  .btn--circle,
  .btn:where([aria-label]:not(.btn--circle-mobile, .btn--plain)):has(.icon) {
    --btn-background: transparent;
    --btn-color: var(--color-ink-subtle);
    --btn-color-hover: var(--color-ink);
    --btn-border-color: transparent;
    --btn-background-hover: var(--color-surface);
    --btn-padding: 0;
    --btn-radius: 50%;
    --btn-font-size: var(--text-medium);

    inline-size: var(--btn-size, 1.75rem);
    block-size: var(--btn-size, 1.75rem);
    aspect-ratio: 1;
    flex-shrink: 0;
    font-weight: 400;
  }

  /* The one filled circle: the send, the post, the save — the primary of a
     row of circles, in the base button's own ink (user, 2026-10-06, the
     chat's ↑ everywhere). After .btn--circle so it wins at equal weight. */
  .btn--filled {
    --btn-background: var(--color-ink);
    --btn-color: var(--color-canvas);
    --btn-color-hover: var(--color-canvas);
    --btn-background-hover: var(--color-ink-subtle);
  }

  /* ——— The one that is on ———
     A quiet button that is the chosen one wears mizu, the selected voice,
     with its paired ink, and keeps it under the pointer: the mode you read
     in, the page you are on, the chosen one of a pair, a label round a
     checked radio. Modes, tabs, segments, folder kinds and the theme switch
     all say "this is the one" through this rule and no other — the current
     one, `aria-current` — and so does a filter chip that narrows, as the page
     it states; a combobox's says it with what is ticked in its popover
     (filters.css). A pressed toggle is not a choice and wears none: the
     editor's T keeps its state for the ear (composers/_formatting_toggle).
     After the circles, so a chosen glyph wears it too. */
  .btn--quiet:is([aria-current]:not([aria-current="false"]), :has(:checked)),
  .combobox--sentence:has([aria-checked="true"]) .combobox__trigger {
    --btn-background: var(--color-selected);
    --btn-color: var(--color-selected-ink);
    --btn-background-hover: var(--color-selected);
    --btn-color-hover: var(--color-selected-ink);
  }

  /* A label dressed as a button hides its radio for the screen reader alone,
     so the ring the radio would have worn goes on the label. */
  label.btn:has(:focus-visible) {
    outline: var(--focus-ring);
    outline-offset: var(--focus-offset);
  }

  /* On a touch screen the disc keeps its drawn size — HANSHI rules 26–28px and
     a review is no place to redraw it — and grows an invisible halo out to
     --tap-target-size instead. Fizzy's .btn--ensure-tap-target-size, except it
     is not a class anyone has to remember: every circle in the app is an
     icon-only action, so every circle wants the finger room.

     Adjacent circles sit 38px apart at gap-half, so two halos overlap by ~6px
     and the later one in the DOM takes that sliver. Still far better than the
     28px each of them offered before.

     .todo-check joins the list from todos.css, the way labels.css lists
     foreign selectors: HANSHI rules the tick and the circles in one paragraph
     and the answer is the same — 20px drawn (16px on a subtask), which is
     under even the WCAG floor, so the ink stays and the finger gets the
     halo. */
  @media (any-hover: none) {
    .btn--circle,
    .btn:where([aria-label]):has(.icon),
    .todo-check {
      /* .btn--circle-mobile is deliberately absent: wide, it is its words
         and already finger-sized; narrow, its own variant block below
         brings the halo along with the circle. */
      position: relative;

      &::before {
        content: "";
        position: absolute;
        inset-block-start: 50%;
        inset-inline-start: 50%;
        translate: -50% -50%;
        inline-size: var(--tap-target-size);
        block-size: var(--tap-target-size);
      }
    }
  }

  /* ——— fizzy's .btn--circle-mobile, on the shell's own break ———
     A labelled header action that folds to its icon on a narrow sheet, so
     the action row still fits beside the heading (the set page's New list).
     Wide, it is its words and the icon stays out of the picture; narrow,
     the label hides — fizzy's own selector — and the button takes the
     circle treatment above, restated here because a @container query cannot
     join that rule's selector list. It is excluded from the icon-circle
     auto-detection (the :not up there): carrying an aria-label and an .icon
     at every width, it would read as a circle on a desk too. A @container
     query and not fizzy's @media: #main narrows just the same when the
     panel opens (AGENTS.md). */
  @container main (width >= 35rem) {
    .btn--circle-mobile > .icon {
      display: none;
    }
  }

  @container main (width < 35rem) {
    .btn--circle-mobile {
      /* Quiet unless it says filled: a board's Add a card stays in ink as a
         circle, because there it is the page's one way in. */
      &:not(.btn--filled) {
        --btn-background: transparent;
        --btn-color: var(--color-ink-subtle);
        --btn-color-hover: var(--color-ink);
        --btn-border-color: transparent;
        --btn-background-hover: var(--color-surface);
      }

      --btn-padding: 0;
      --btn-radius: 50%;
      --btn-font-size: var(--text-medium);

      inline-size: var(--btn-size, 1.75rem);
      block-size: var(--btn-size, 1.75rem);
      aspect-ratio: 1;
      flex-shrink: 0;

      > span:not(.icon) {
        display: none;
      }

      /* The same halo every circle grows under a finger (the block above). */
      @media (any-hover: none) {
        position: relative;

        &::before {
          content: "";
          position: absolute;
          inset-block-start: 50%;
          inset-inline-start: 50%;
          translate: -50% -50%;
          inline-size: var(--tap-target-size);
          block-size: var(--tap-target-size);
        }
      }
    }
  }

  /* Action rows right-align, primary outermost. `.modal__actions` used to be
     listed here so a dialog's row would stop restating all five declarations;
     it is gone now, because a dialog's row simply IS this row — the forms say
     `.actions` and the only thing that was ever a modal's own, the space above
     it, comes from the dialog's own stack (2026-08-29). */
  .actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--inline-space);
  }

  /* Except a SUB-form's row, which sits left with its own fields — BC's
     shape. Right-aligned it landed on the sheet form's Save row directly
     beneath and read as a duplicate (user, 2026-08-11). Primary outermost
     still holds: on a left row the outer edge is the left. */
  .actions--nested {
    justify-content: flex-start;
    /* A top-level row is spaced by the .stack it sits in; a nested one sits
       inside a <dd> with nothing between it and the field above, so it clears
       that field itself. It showed the moment the sub-form's date grew a box
       and the buttons sat on its edge (user, 2026-08-29). */
    margin-block-start: var(--block-space-half);
  }

  /* A row with TWO submits lists the primary first so return picks it —
     implicit submission takes the first submit button in the form — then
     reverses so the primary still stands outermost. Every add-form with a
     "save and add another" wears it, dialogs included; the sheet needed it the
     day the composer grew Save a draft. */
  .actions--reversed {
    flex-direction: row-reverse;
    justify-content: flex-start;
  }

  /* A keyboard hint sits far left: the auto margin eats the free space
     beside it. Its voice is the one every hint speaks (labels.css). */
  .actions kbd {
    margin-inline-end: auto;
  }
}
