/*
 * base.css: element defaults, typography, focus, motion policy. Values come from tokens.css only.
 */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background-color: var(--bm-color-paper);
  color: var(--bm-color-ink);
  font-family: var(--bm-font-ui);
  font-size: var(--bm-text-base);
  line-height: var(--bm-leading-body);
  font-variant-numeric: var(--bm-font-numeric);
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

/* In :where() so the heading rhythm below (and any class) can set margins: as a plain element
   rule this outweighed the zero-specificity space-above rules (review 1.11). */
:where(h1, h2, h3, h4) {
  margin: 0;
  font-family: var(--bm-font-display);
  font-weight: var(--bm-weight-display);
  line-height: var(--bm-leading-tight);
  letter-spacing: var(--bm-tracking-display);
  text-wrap: balance;
}

p,
ul,
ol,
dl,
dd,
figure {
  margin: 0;
}

p {
  text-wrap: pretty;
}

a {
  color: var(--bm-color-link);
  text-decoration-thickness: var(--bm-rule-hair);
  text-underline-offset: 0.18em;
  transition: color var(--bm-duration-fast) var(--bm-ease-standard);
}

a:hover {
  color: var(--bm-color-link-hover);
  text-decoration-thickness: var(--bm-rule-thick);
}

img,
svg {
  display: block;
  max-width: 100%;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

table {
  border-collapse: collapse;
}

hr,
.bm-rule {
  border: 0;
  border-top: var(--bm-rule-hair) solid var(--bm-color-rule);
  margin: var(--bm-space-lg) 0;
}

/* ---- Focus: one designed ring everywhere, offset so it never hides the element ----------- */

:focus-visible {
  outline: var(--bm-focus-width) solid var(--bm-focus-ring);
  outline-offset: var(--bm-focus-offset);
}

:focus:not(:focus-visible) {
  outline: none;
}

.bm-main:focus {
  outline: none;
}

/* ---- Accessibility helpers --------------------------------------------------------------- */

.bm-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.bm-skip-link {
  position: absolute;
  top: var(--bm-space-xs);
  left: var(--bm-space-xs);
  z-index: var(--bm-z-skip);
  padding: var(--bm-space-xs) var(--bm-space-md);
  background: var(--bm-color-ink);
  color: var(--bm-color-paper);
  font-weight: var(--bm-weight-semibold);
  text-decoration: none;
  transform: translateY(-200%);
  transition: transform var(--bm-duration-fast) var(--bm-ease-out);
}

.bm-skip-link:focus-visible {
  transform: translateY(0);
  outline-color: var(--bm-focus-ring-on-dark);
}

/* ---- Motion policy: everything off for people who ask --------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* htmx swap fade (opacity only) */
.htmx-swapping {
  opacity: 0;
  transition: opacity var(--bm-duration-fast) var(--bm-ease-standard);
}

.htmx-settling {
  opacity: 1;
  transition: opacity var(--bm-duration-fast) var(--bm-ease-standard);
}

.htmx-request.bm-busy,
.htmx-request .bm-busy {
  opacity: 0.6;
}

/* ---- Shared defaults for bare elements (visual review §3, Sprint 6 quick wins) ------------
 * Every rule here is wrapped in :where() (no specificity) and only targets elements without a
 * class, so a component or page class always wins. They remove the browser defaults that made
 * pages look unfinished (review 1.3-1.5, 1.11) without touching any page template.
 */

/* One field style (review 1.5): bare inputs, selects and textareas look like .bm-field's. */
:where(
    input:not([class], [type="checkbox"], [type="radio"], [type="hidden"], [type="submit"],
      [type="button"], [type="reset"], [type="file"], [type="range"], [type="color"],
      [type="image"]),
    select:not([class]),
    textarea:not([class])
  ) {
  max-inline-size: 100%;
  min-block-size: var(--bm-control-h);
  padding: var(--bm-space-xs) var(--bm-space-sm);
  background-color: var(--bm-color-paper-raised);
  border: 0;
  border-block-end: var(--bm-rule-thick) solid var(--bm-color-rule-strong);
  border-radius: var(--bm-radius-sm) var(--bm-radius-sm) 0 0;
  transition: border-color var(--bm-duration-fast) var(--bm-ease-standard);
}

:where(input:not([class]), select:not([class]), textarea:not([class])):hover {
  border-block-end-color: var(--bm-color-ink);
}

:where(input:not([class]), select:not([class]), textarea:not([class])):focus-visible {
  border-block-end-color: var(--bm-focus-ring);
}

/* Compact fields inside table rows and filter bars (.bm-filter-bar or .bm-compact). Checkboxes and
   radios keep their 20px box: the compact height would stretch them into tall boxes. */
:where(td, th, .bm-compact, .bm-filter-bar) :where(input:not([class], [type="checkbox"], [type="radio"]), select:not([class])) {
  min-block-size: var(--bm-control-h-compact);
  padding-block: var(--bm-space-2xs);
}

/* One 20px checkbox and radio (review 1.4): drawn, filled with the accent when checked. The tick
   is a clip-path, so no image and no inline style (strict CSP). */
:where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])) {
  appearance: none;
  display: inline-grid;
  place-content: center;
  flex-shrink: 0;
  inline-size: var(--bm-check-size);
  block-size: var(--bm-check-size);
  margin: 0;
  vertical-align: middle;
  background-color: var(--bm-color-paper-raised);
  border: var(--bm-rule-thick) solid var(--bm-color-ink-muted);
  border-radius: var(--bm-radius-sm);
  cursor: pointer;
  transition:
    background-color var(--bm-duration-fast) var(--bm-ease-standard),
    border-color var(--bm-duration-fast) var(--bm-ease-standard);
}

:where(input[type="radio"]:not([class])) {
  border-radius: var(--bm-radius-pill);
}

:where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class]))::before {
  content: "";
  inline-size: calc(var(--bm-check-size) * 0.55);
  block-size: calc(var(--bm-check-size) * 0.55);
  background-color: var(--bm-color-accent-ink);
  clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
  transform: scale(0);
  transition: transform var(--bm-duration-fast) var(--bm-ease-out);
}

:where(input[type="radio"]:not([class]))::before {
  border-radius: var(--bm-radius-pill);
  clip-path: none;
}

:where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])):hover {
  border-color: var(--bm-color-ink);
}

:where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])):checked {
  background-color: var(--bm-color-accent);
  border-color: var(--bm-color-accent);
}

:where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])):checked::before {
  transform: scale(1);
}

:where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])):disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

/* A label wrapping its checkbox is one row, and a 44px tap area on touch screens. */
:where(label:has(> input[type="checkbox"]:not([class]), > input[type="radio"]:not([class]))) {
  display: inline-flex;
  align-items: center;
  gap: var(--bm-space-xs);
  cursor: pointer;
}

@media (pointer: coarse) {
  :where(label:has(> input[type="checkbox"]:not([class]), > input[type="radio"]:not([class]))) {
    min-block-size: var(--bm-control-h);
  }
}

@media (forced-colors: active) {
  :where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])) {
    border-color: CanvasText;
  }

  :where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])):checked {
    background-color: Highlight;
  }

  :where(input[type="checkbox"]:not([class]), input[type="radio"]:not([class])):checked::before {
    background-color: HighlightText;
  }
}

/* Fieldsets are sections, not boxes (review 1.3): a top rule with the legend under it as the
   group heading (floated, so it never cuts into the line). */
:where(fieldset:not([class])) {
  min-inline-size: 0;
  margin: 0;
  padding: var(--bm-space-sm) 0 0;
  border: 0;
  border-block-start: var(--bm-rule-hair) solid var(--bm-color-rule);
}

:where(fieldset:not([class]) > legend:not([class])) {
  float: inline-start;
  inline-size: 100%;
  margin-block-end: var(--bm-space-xs);
  padding: 0;
  font-weight: var(--bm-weight-semibold);
}

:where(fieldset:not([class]) > legend:not([class]) + *) {
  clear: both;
}

/* Disclosures use the nav's chevron, not the browser's triangle (review 1.3). */
:where(summary:not([class])) {
  display: inline-flex;
  align-items: center;
  gap: var(--bm-space-xs);
  min-block-size: var(--bm-control-h);
  font-weight: var(--bm-weight-semibold);
  cursor: pointer;
}

/* The chevron. A summary with its own class opts in with .bm-chevron and keeps its own layout;
   the chevron follows the text (gap 10 in the shared-CSS list). */
:where(summary:not([class]), .bm-chevron) {
  list-style: none;
}

:where(summary:not([class]), .bm-chevron)::-webkit-details-marker {
  display: none;
}

:where(summary:not([class]), .bm-chevron)::after {
  content: "";
  inline-size: 0.45em;
  block-size: 0.45em;
  border-right: var(--bm-rule-thick) solid currentColor;
  border-bottom: var(--bm-rule-thick) solid currentColor;
  transform: translateY(-25%) rotate(45deg);
  transition: transform var(--bm-duration-fast) var(--bm-ease-out);
}

:where(.bm-chevron)::after {
  display: inline-block;
  align-self: center;
  margin-inline-start: var(--bm-space-2xs);
}

:where(details[open] > :is(summary:not([class]), .bm-chevron))::after {
  transform: translateY(25%) rotate(-135deg);
}

/* No bullets next to checkboxes and links (review 1.3): lists inside forms and navs, and lists
   whose items are a single link, label or checkbox, are rows. Prose lists keep their bullets. */
:where(
    form ul:not([class]),
    form ol:not([class]),
    nav ul:not([class]),
    ul:not([class]):has(> li > :is(a, label, input):only-child)
  ) {
  padding: 0;
  list-style: none;
}

/* Three heading levels with fixed space above (review 1.11): page (h1), section (h2), group (h3).
   The first heading in its block keeps no space above. Display-font headings are never
   underlined; their links underline on hover only. */
:where(.bm-page__content) :where(h2:not(:first-child)) {
  margin-block-start: var(--bm-space-before-section);
}

:where(.bm-page__content) :where(h3:not(:first-child)) {
  margin-block-start: var(--bm-space-before-group);
}

:where(.bm-page__content) :where(h2) {
  margin-block-end: var(--bm-space-sm);
  font-size: var(--bm-heading-section);
}

:where(.bm-page__content) :where(h3) {
  margin-block-end: var(--bm-space-xs);
  font-size: var(--bm-heading-group);
}

/* A title is not a section: a heading in a <header> (drawer and card heads) or right after an
   eyebrow keeps no rhythm margins, so it stays tight under its eyebrow. */
:where(.bm-page__content) :where(header > :is(h2, h3), .bm-eyebrow + :is(h2, h3)) {
  margin-block: 0;
}

:where(.bm-page__content > section:not(:first-child)) {
  margin-block-start: var(--bm-space-before-page);
}

:where(h1, h2, h3, h4) a {
  text-decoration: none;
}

:where(h1, h2, h3, h4) a:hover {
  text-decoration: underline;
}

@media (prefers-reduced-motion: reduce) {
  :where(input:not([class]), summary:not([class]))::before,
  :where(input:not([class]), summary:not([class]), .bm-chevron)::after {
    transition: none;
  }
}
