/* Base layout and components. Every value comes from tokens.css. */
*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-size: var(--text-sm);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3 {
  font-family: var(--font-heading);
  line-height: 1.25;
  color: var(--fg);
}

h1 {
  font-size: var(--text-title);
  font-weight: 700;
  letter-spacing: -0.025em;
  margin: 0 0 var(--space-1);
}

h2 {
  font-size: var(--text-md);
  font-weight: 600;
  margin: 0 0 var(--space-3);
}

h3 {
  font-size: var(--text-sm);
  font-weight: 600;
  margin: var(--space-3) 0 var(--space-2);
}

p {
  margin: 0 0 var(--space-2);
}

a {
  color: var(--accent-text);
  text-underline-offset: 2px;
}

code,
.mono {
  font-family: var(--font-mono);
  font-size: 0.92em;
}

.muted {
  color: var(--fg-muted);
}

.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: var(--space-3);
  top: -100px;
  z-index: 100;
  background: var(--card);
  color: var(--fg);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-button);
  box-shadow: var(--shadow-md);
}

.skip-link:focus {
  top: var(--space-3);
}

/* Focus ring: 3 px on keyboard focus, everywhere (RNF-10). */
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 3px solid var(--ring);
  outline-offset: 2px;
}

/* ------------------------------------------------------------------------------------------
 * Motion. Transitions are enabled after the first paint (app.js adds .motion-ready), so the
 * restored theme and sidebar state never animate on load.
 * ---------------------------------------------------------------------------------------- */
@keyframes fade-in {
  from {
    opacity: 0;
  }
}

@keyframes slide-up {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

@keyframes scale-in {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
}

.page > * {
  animation: slide-up var(--duration-enter) var(--ease-out) both;
}

.page > :nth-child(2) {
  animation-delay: 40ms;
}

.page > :nth-child(3) {
  animation-delay: 80ms;
}

.page > :nth-child(n + 4) {
  animation-delay: 120ms;
}

/* Staggered entrance: children enter 80 ms apart, in document order. */
.stagger > :nth-child(2) {
  --i: 1;
}

.stagger > :nth-child(3) {
  --i: 2;
}

.stagger > :nth-child(n + 4) {
  --i: 3;
}

.stagger > * {
  animation: slide-up var(--duration-enter) var(--ease-out) both;
  animation-delay: calc(var(--i, 0) * 80ms);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    animation-delay: 0ms !important;
    transition-duration: 1ms !important;
  }
}

/* ------------------------------------------------------------------------------------------
 * App shell: sidebar, header and content.
 * ---------------------------------------------------------------------------------------- */
.shell {
  min-height: 100vh;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--sidebar-fg);
  font-family: var(--font-heading);
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
  min-width: 0;
}

.brand__mark {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: var(--radius-input);
  background: linear-gradient(135deg, var(--color-accent), color-mix(in srgb, var(--color-accent) 55%, var(--color-accent-soft)));
  color: var(--color-white);
  font-size: var(--text-xs);
  letter-spacing: 0.04em;
  box-shadow: 0 4px 12px -4px color-mix(in srgb, var(--color-accent) 70%, transparent);
}

.brand__text {
  font-size: var(--text-sm);
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand__suffix {
  color: var(--color-accent-soft);
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  color: var(--sidebar-fg);
  border-right: 1px solid var(--sidebar-border);
}

.sidebar__brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-height);
  padding: 0 var(--space-3);
  border-bottom: 1px solid var(--sidebar-border);
}

.sidebar__nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-3) 12px;
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}

.sidebar__nav:hover {
  scrollbar-color: rgb(255 255 255 / 18%) transparent;
}

.nav-group + .nav-group {
  margin-top: 12px;
}

.nav-group__toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 6px 12px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--sidebar-muted);
  font: inherit;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
}

.nav-group__toggle:hover {
  color: color-mix(in srgb, var(--sidebar-fg) 70%, transparent);
}

.nav-group__toggle .icon {
  width: 12px;
  height: 12px;
}

.nav-group__toggle[aria-expanded="false"] .icon {
  transform: rotate(-90deg);
}

/* Accordion: the body collapses by animating its grid row, never its height. */
.nav-group__body {
  display: grid;
  grid-template-rows: 1fr;
}

.nav-group__body > div {
  overflow: hidden;
  min-height: 0;
  display: grid;
  gap: 2px;
}

.nav-group__toggle {
  margin-bottom: 4px;
}

.nav-group__toggle[aria-expanded="false"] + .nav-group__body {
  grid-template-rows: 0fr;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 36px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: color-mix(in srgb, var(--sidebar-fg) 70%, transparent);
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
}

.nav-item:hover {
  background: var(--sidebar-hover);
  color: var(--sidebar-fg);
}

.nav-item[aria-current="page"] {
  background: var(--sidebar-active);
  color: var(--sidebar-active-fg);
}

.sidebar__user {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px var(--space-3);
  border-top: 1px solid var(--sidebar-border);
}

.sidebar__user form {
  margin: 0 0 0 auto;
}

.sidebar__user-text {
  display: grid;
  min-width: 0;
  line-height: 1.3;
}

.sidebar__user-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar__user-role {
  font-size: var(--text-xs);
  color: var(--sidebar-muted);
}

.sidebar__collapse {
  position: absolute;
  top: 80px;
  right: -12px;
  display: none;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--card);
  color: var(--fg);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}

.sidebar__collapse:hover {
  background: var(--hover);
}

.sidebar__collapse .icon {
  width: 14px;
  height: 14px;
}

.sidebar__close {
  color: var(--sidebar-fg);
}

.shell__main {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-width: 0;
}

.header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: var(--header-height);
  padding: 0 var(--space-3);
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
}

.header__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
}

.header__divider {
  width: 1px;
  height: 24px;
  margin: 0 var(--space-1);
  background: var(--border);
}

.page {
  width: 100%;
  max-width: 1440px;
  padding: var(--space-3);
}

.page > * + * {
  margin-top: var(--gutter);
}

/* A page title and the line under it read as one header. */
.page > h1 + p {
  margin-top: 0;
  color: var(--fg-muted);
}

.backdrop {
  position: fixed;
  inset: 0;
  z-index: 35;
  background: rgb(15 9 25 / 50%);
  opacity: 0;
  pointer-events: none;
}

/* Mobile: the sidebar slides over the page. */
/* Off-canvas sidebar breakpoint; static/app.js mirrors it in `mobile` (keep both in sync). */
@media (max-width: 1023.98px) {
  .sidebar {
    transform: translateX(-100%);
    max-width: 85vw;
  }

  .nav-open .sidebar {
    transform: none;
    box-shadow: var(--shadow-lg);
  }

  .nav-open .backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .header .button span {
    display: none;
  }
}

/* Desktop: fixed sidebar, collapsible to icons. */
@media (min-width: 1024px) {
  .shell__main {
    margin-left: var(--sidebar-width);
  }

  .header {
    padding: 0 var(--gutter);
  }

  .page {
    padding: var(--gutter);
  }

  .header .header__menu,
  .sidebar .sidebar__close {
    display: none;
  }

  .sidebar__collapse {
    display: grid;
  }

  [data-sidebar="collapsed"] .sidebar {
    width: var(--sidebar-collapsed);
  }

  [data-sidebar="collapsed"] .shell__main {
    margin-left: var(--sidebar-collapsed);
  }

  [data-sidebar="collapsed"] .sidebar__collapse .icon {
    transform: rotate(180deg);
  }

  [data-sidebar="collapsed"] .sidebar__brand {
    justify-content: center;
    padding: 0;
  }

  [data-sidebar="collapsed"] .brand__text,
  [data-sidebar="collapsed"] .nav-item span,
  [data-sidebar="collapsed"] .sidebar__user-text,
  [data-sidebar="collapsed"] .sidebar__user form {
    display: none;
  }

  [data-sidebar="collapsed"] .nav-group__toggle {
    justify-content: center;
    font-size: 0;
  }

  [data-sidebar="collapsed"] .nav-item {
    justify-content: center;
    padding: 8px;
  }

  [data-sidebar="collapsed"] .sidebar__user {
    justify-content: center;
    padding: 12px 0;
  }
}

.motion-ready .sidebar {
  transition: width var(--duration-slow) ease-in-out, transform var(--duration-slow) ease-in-out;
}

.motion-ready .shell__main {
  transition: margin-left var(--duration-slow) ease-in-out;
}

.motion-ready .backdrop {
  transition: opacity var(--duration-slow) ease-in-out;
}

.motion-ready .sidebar__collapse .icon {
  transition: transform var(--duration-slow) var(--ease);
}

.motion-ready .nav-group__body {
  transition: grid-template-rows var(--duration-base) var(--ease);
}

.motion-ready .nav-group__toggle .icon {
  transition: transform var(--duration-base) var(--ease);
}

.motion-ready :where(.nav-item, .nav-group__toggle, .icon-button, .sidebar__collapse, .button, .avatar--button, .menu-item, .menu__action, .menu__footer, .table tbody tr, input, select, textarea, .badge) {
  transition: color var(--duration-fast) var(--ease), background-color var(--duration-fast) var(--ease),
    border-color var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease);
}

.motion-ready .nav-item {
  transition-duration: var(--duration-base);
}

/* ------------------------------------------------------------------------------------------
 * Header controls: icon buttons, avatar, dropdown menus.
 * ---------------------------------------------------------------------------------------- */
.icon-button {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--fg-muted);
  cursor: pointer;
}

.icon-button:hover,
.icon-button[aria-expanded="true"] {
  background: var(--hover);
  color: var(--fg);
}

.icon-button--sidebar {
  color: var(--sidebar-muted);
}

.icon-button--sidebar:hover {
  background: var(--sidebar-hover);
  color: var(--sidebar-fg);
}

.icon-button__dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 8px;
  height: 8px;
  border: 2px solid var(--bg);
  border-radius: var(--radius-full);
  background: var(--color-danger);
}

.theme-icon {
  display: grid;
}

[data-theme="dark"] .theme-icon--light,
:root:not([data-theme="dark"]) .theme-icon--dark {
  display: none;
}

.avatar {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: var(--radius-full);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent-text);
  font-size: var(--text-xs);
  font-weight: 600;
}

.avatar--solid {
  width: 36px;
  height: 36px;
  background: var(--color-accent);
  color: var(--color-white);
}

.avatar--button {
  margin-left: var(--space-1);
  border: 0;
  font: inherit;
  font-size: var(--text-xs);
  font-weight: 600;
  cursor: pointer;
}

.avatar--button:hover,
.avatar--button[aria-expanded="true"] {
  background: color-mix(in srgb, var(--accent) 20%, transparent);
}

.dropdown {
  position: relative;
}

.menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 50;
  width: 240px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  background: var(--card);
  box-shadow: var(--shadow-lg);
  transform-origin: top right;
  animation: scale-in var(--duration-fast) var(--ease-out);
}

.menu[hidden] {
  display: none;
}

.menu--wide {
  width: min(340px, calc(100vw - 32px));
}

.menu__header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 12px var(--space-3);
  border-bottom: 1px solid var(--border);
}

.menu__header--stack {
  display: grid;
  gap: 0;
}

.menu__title {
  font-weight: 600;
}

.menu__subtitle {
  font-size: var(--text-xs);
  color: var(--fg-muted);
}

.menu__empty {
  margin: 0;
  padding: var(--space-3);
  color: var(--fg-muted);
}

.menu__list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 360px;
  overflow-y: auto;
}

.menu__list li + li {
  border-top: 1px solid var(--border);
}

.menu-item {
  display: flex;
  gap: 12px;
  padding: 12px var(--space-3);
  color: var(--fg);
  text-decoration: none;
}

.menu-item:hover {
  background: var(--hover);
}

.menu-item__icon {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: var(--radius-full);
  background: color-mix(in srgb, var(--color-warning) 14%, transparent);
  color: var(--color-warning);
}

.menu-item__icon .icon {
  width: 16px;
  height: 16px;
}

.menu-item__text {
  display: grid;
  min-width: 0;
  line-height: 1.35;
}

.menu-item__title {
  font-weight: 600;
}

.menu-item__meta {
  color: var(--fg-muted);
}

.menu-item__time {
  margin-top: 2px;
  font-size: var(--text-2xs);
  color: var(--fg-muted);
}

.menu__footer {
  display: block;
  padding: 10px var(--space-3);
  border-top: 1px solid var(--border);
  text-align: center;
  font-weight: 500;
  text-decoration: none;
}

.menu__footer:hover {
  background: var(--hover);
}

.menu form {
  margin: 0;
}

.menu__action {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px var(--space-3);
  border: 0;
  background: none;
  color: var(--fg);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.menu__action:hover {
  background: var(--hover);
}

.pill {
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--radius-full);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent-text);
  font-size: var(--text-2xs);
  font-weight: 600;
}

/* ------------------------------------------------------------------------------------------
 * Components.
 * ---------------------------------------------------------------------------------------- */
.card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: var(--gutter);
  box-shadow: var(--shadow-sm);
}

.motion-ready .card {
  transition: box-shadow var(--duration-slow) var(--ease), border-color var(--duration-slow) var(--ease);
}

.card:hover {
  box-shadow: var(--shadow-md);
  border-color: color-mix(in srgb, var(--accent) 22%, var(--border));
}

.card > :last-child {
  margin-bottom: 0;
}

.card--narrow {
  max-width: 28rem;
  margin-inline: auto;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-button);
  background: var(--accent);
  color: var(--accent-fg);
  font: inherit;
  font-weight: 500;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}

.button:hover {
  background: color-mix(in srgb, var(--accent) 88%, var(--color-ink));
}

.button .icon {
  width: 16px;
  height: 16px;
}

.button--small {
  min-height: 32px;
  padding: 6px 12px;
  font-size: var(--text-xs);
}

.button--danger {
  background: var(--color-danger);
  color: var(--color-white);
}

.button--danger:hover {
  background: color-mix(in srgb, var(--color-danger) 85%, black);
}

.button--ghost,
.button--ghost-dark {
  background: var(--card);
  border-color: var(--border-strong);
  color: var(--fg);
  box-shadow: var(--shadow-xs);
}

.button--ghost:hover,
.button--ghost-dark:hover {
  background: var(--hover);
}

/* State badge: tone color on a tint of itself, always with its text label. */
.badge {
  --tone: var(--fg-muted);
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--tone) 13%, transparent);
  color: var(--tone);
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 1.5;
  white-space: nowrap;
}

.badge--success {
  --tone: var(--color-success);
}

.badge--warning {
  --tone: var(--color-warning);
}

.badge--danger {
  --tone: var(--color-danger);
}

.badge--info {
  --tone: var(--color-info);
}

.badge--neutral {
  --tone: var(--fg-muted);
}

/* Forms: one field per row, visible labels, keyboard focus ring (RNF-10). */
.form {
  display: grid;
  gap: var(--space-2);
}

/* A plain fieldset inside a form stacks its fields like the form; with a visible legend it
 * is a bordered group (brand pillars), with a hidden one it is invisible (piece details). */
.form fieldset:not([class]) {
  display: grid;
  gap: var(--space-2);
  min-width: 0;
  margin: 0;
  padding: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-input);
}

.form fieldset:not([class]) legend:not(.visually-hidden) {
  padding-inline: var(--space-1);
  font-weight: 600;
}

.form fieldset:not([class]):has(> legend.visually-hidden) {
  padding: 0;
  border: 0;
}

.form label {
  font-weight: 500;
}

.form label:not(.check) {
  margin-top: var(--space-1);
}

input,
select,
textarea {
  min-height: 38px;
  min-width: 0;
  padding: 7px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-input);
  background: var(--card);
  color: var(--fg);
  font: inherit;
  box-shadow: var(--shadow-xs);
}

textarea {
  resize: vertical;
}

input[type="checkbox"],
input[type="radio"] {
  min-height: 0;
  width: 16px;
  height: 16px;
  padding: 0;
  accent-color: var(--accent);
  box-shadow: none;
}

input::placeholder,
textarea::placeholder {
  color: color-mix(in srgb, var(--fg-muted) 70%, transparent);
}

input:hover,
select:hover,
textarea:hover {
  border-color: color-mix(in srgb, var(--accent) 35%, var(--border-strong));
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}

input[type="checkbox"]:focus-visible,
input[type="radio"]:focus-visible {
  outline: 3px solid var(--ring);
  outline-offset: 2px;
  box-shadow: none;
}

.form .button {
  justify-self: start;
  margin-top: var(--space-2);
}

.form-hint {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--fg-muted);
}

.form-error {
  margin: 0 0 var(--space-2);
  border: 1px solid color-mix(in srgb, var(--color-danger) 35%, transparent);
  border-radius: var(--radius-input);
  background: color-mix(in srgb, var(--color-danger) 9%, transparent);
  color: var(--color-danger);
  padding: 10px var(--space-3);
  font-weight: 500;
  animation: scale-in var(--duration-base) var(--ease-out);
}

.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  align-items: center;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
}

.fieldset {
  display: grid;
  gap: var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-input);
  padding: var(--space-3);
  margin: 0;
  min-width: 0;
}

.fieldset legend,
.checks legend {
  font-weight: 600;
  padding-inline: var(--space-1);
}

.checks {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-3);
  border: 0;
  padding: 0;
  margin: 0;
}

.form .check {
  font-weight: 400;
  display: inline-flex;
  gap: var(--space-2);
  align-items: center;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

/* Small forms inside a table row: payments to void, extensions (RF-06). */
.form--inline {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.form--inline .button {
  margin-top: 0;
}

/* Permanent warnings, such as missing Meta access (RF-07): color and text, never color alone. */
.notice {
  --tone: var(--color-warning);
  border: 1px solid color-mix(in srgb, var(--tone) 40%, transparent);
  border-left: 4px solid var(--tone);
  border-radius: var(--radius-input);
  background: color-mix(in srgb, var(--tone) 9%, var(--card));
  color: var(--fg);
  padding: 12px var(--space-3);
  font-weight: 500;
}

.notice--warning {
  --tone: var(--color-warning);
}

/* RN-18: the technical pause notice. */
.notice--danger {
  --tone: var(--color-danger);
}

/* Tables sit in a card; they scroll inside it on narrow screens (360 px, RNF-10). */
/* Positioned, so visually hidden header text stays inside the scroll box. */
.table-scroll {
  position: relative;
  overflow-x: auto;
  margin-inline: calc(var(--gutter) * -1);
  padding-inline: var(--gutter);
}

.table {
  width: 100%;
  border-collapse: collapse;
}

.table th,
.table td {
  text-align: left;
  padding: 12px var(--space-3);
  border-bottom: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
  vertical-align: middle;
}

.table th:first-child,
.table td:first-child {
  padding-left: 0;
}

.table th:last-child,
.table td:last-child {
  padding-right: 0;
}

.table thead th {
  color: var(--fg-muted);
  font-size: var(--text-xs);
  font-weight: 500;
  white-space: nowrap;
  border-bottom-color: var(--border);
}

.table tbody tr:last-child td {
  border-bottom: 0;
}

.table tbody tr:hover {
  background: color-mix(in srgb, var(--muted) 70%, transparent);
}

.table td a {
  font-weight: 500;
}

.table__subrow td {
  font-size: 0.92em;
  border-top: none;
}

/* ------------------------------------------------------------------------------------------
 * List screens: page header with a breadcrumb, a table flush with its card, the creation form
 * beside the list.
 * ---------------------------------------------------------------------------------------- */
.page-header {
  margin-bottom: var(--gutter);
}

.page-header h1 {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) 12px;
  margin: 0;
}

/* A state badge beside the title reads at the title's height. */
.page-header__status {
  display: inline-flex;
}

.page-header__meta {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin: var(--space-2) 0 0;
  color: var(--fg-muted);
  font-size: var(--text-sm);
}

.page-header h1 .piece {
  display: inline-block;
  padding: 2px 10px;
  font-size: var(--text-lg);
}

.page-header h1 .badge {
  font-family: var(--font-body);
  letter-spacing: 0;
}

.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-1);
  color: var(--fg-muted);
  font-size: var(--text-xs);
}

.breadcrumb a {
  color: inherit;
  text-decoration: none;
}

.breadcrumb a:hover {
  color: var(--accent-text);
}

.breadcrumb > * + *::before {
  content: "/";
  margin-right: var(--space-2);
  color: var(--border-strong);
}

.breadcrumb [aria-current] {
  color: var(--fg);
  font-weight: 500;
}

.split {
  display: grid;
  gap: var(--space-3);
  align-items: start;
}

@media (min-width: 1200px) {
  .split {
    grid-template-columns: minmax(0, 2fr) minmax(18rem, 1fr);
  }

  .split > :last-child {
    position: sticky;
    top: calc(var(--header-height) + var(--gutter));
  }
}

/* The table runs edge to edge; notes and empty states keep the card padding. */
.card--table {
  padding: 0;
  overflow: hidden;
}

.card--table > p {
  margin: 0;
  padding: var(--space-3) var(--gutter);
}

.card--table > p + .table-scroll {
  border-top: 1px solid var(--border);
}

.card--table .table-scroll {
  margin-inline: 0;
  padding-inline: 0;
}

.card--table .table th:first-child,
.card--table .table td:first-child {
  padding-left: var(--gutter);
}

.card--table .table th:last-child,
.card--table .table td:last-child {
  padding-right: var(--gutter);
}

.card--table .table thead th {
  background: var(--muted);
  padding-block: 10px;
}

.card--table .table td {
  padding-block: 14px;
}

.person {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 500;
}

.avatar--sm {
  width: 30px;
  height: 30px;
  font-size: var(--text-2xs);
}

.table td a {
  color: var(--fg);
  text-decoration: none;
}

.table td a:hover {
  color: var(--accent-text);
  text-decoration: underline;
}

/* Row actions are soft: a tint of their tone, solid only on hover. */
.table .button--small {
  --tone: var(--accent);
  background: color-mix(in srgb, var(--tone) 12%, transparent);
  border-color: transparent;
  color: var(--accent-text);
  box-shadow: none;
}

.table .button--small:hover {
  background: var(--tone);
  color: var(--accent-fg);
}

.table .button--danger {
  --tone: var(--color-danger);
  color: var(--color-danger);
}

.table .button--ghost-dark {
  --tone: var(--fg-muted);
  color: var(--fg);
}

.table .button--ghost-dark:hover {
  color: var(--card);
}

/* Dark theme: the lightened danger red needs dark text on a solid fill. */
:root[data-theme="dark"] .button--danger,
:root[data-theme="dark"] .table .button--danger:hover {
  color: #1f0a09;
}

:root[data-theme="dark"] .table .button--danger {
  color: var(--color-danger);
}

/* Names, dates and inline row forms stay on one line; the table scrolls instead. */
.person,
.table .inline-form,
.table [data-entity] td:not(.snapshot-cell) {
  white-space: nowrap;
}

.table .inline-form,
.table .actions {
  flex-wrap: nowrap;
}

.table td.actions {
  display: flex;
  gap: var(--space-1);
  justify-content: flex-end;
}

.table .inline-form select {
  width: auto;
}

.snapshot-cell {
  min-width: 16rem;
}

.snapshot-cell .snapshot {
  display: block;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  background: var(--muted);
  color: var(--fg-muted);
  font-size: var(--text-2xs);
  line-height: 1.5;
}

/* Audit filters: one row of labelled fields on wide screens. */
.filters label {
  color: var(--fg-muted);
  font-size: var(--text-xs);
  font-weight: 500;
}

.filters select,
.filters input {
  width: auto;
  min-width: 10rem;
  margin-right: var(--space-2);
}

/* Audit snapshots are JSON on one line; they wrap inside the table cell. */
.snapshot {
  word-break: break-word;
  font-size: 0.85em;
}

.summary {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 10px var(--space-3);
  margin: 0;
}

.summary dt {
  color: var(--fg-muted);
  font-weight: 500;
}

.summary dd {
  margin: 0;
}

.totp-qr {
  display: block;
  margin: var(--space-3) auto;
  padding: var(--space-2);
  border-radius: var(--radius-input);
  background: var(--color-white);
  image-rendering: pixelated;
}

.totp-secret {
  word-break: break-all;
}

/* Editorial calendar (RF-09): pieces carry their format as color and text. */
.piece {
  --format: var(--color-accent);
  display: block;
  overflow: hidden;
  padding: 2px 6px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--format) 13%, transparent);
  color: var(--fg);
  font-size: var(--text-xs);
  font-weight: 500;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-left: 3px solid var(--format);
}

.piece--reel {
  --format: var(--color-accent);
}

.piece--carousel {
  --format: var(--color-info);
}

.piece--story {
  --format: var(--color-green);
}

.calendar {
  width: 100%;
  min-width: 56rem;
  border-collapse: collapse;
  table-layout: fixed;
}

.calendar th {
  padding: 0 0 var(--space-2);
  color: var(--fg-muted);
  font-size: var(--text-xs);
  font-weight: 500;
  text-align: center;
}

.calendar td {
  border: 1px solid var(--border);
  vertical-align: top;
  padding: 8px;
}

.calendar__day {
  height: 7rem;
}

.motion-ready .calendar__day {
  transition: background-color var(--duration-fast) var(--ease);
}

.calendar__day:hover {
  background: color-mix(in srgb, var(--muted) 70%, transparent);
}

.calendar__day--outside {
  background: color-mix(in srgb, var(--muted) 60%, transparent);
  color: var(--fg-muted);
}

/* RN-42..RN-45: days and weeks short of the cadence, with what is missing in text. */
.calendar__day--short {
  box-shadow: inset 0 0 0 2px var(--color-warning);
}

.calendar__date {
  font-weight: 600;
}

.calendar__warning {
  margin: var(--space-1) 0;
  color: var(--color-warning);
  font-size: var(--text-xs);
  font-weight: 600;
}

.calendar__week-gap td {
  border-color: color-mix(in srgb, var(--color-warning) 50%, var(--border));
  background: color-mix(in srgb, var(--color-warning) 8%, transparent);
}

.calendar__pieces {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 6px;
}

/* A piece and its state badge stay inside the day; long labels end with an ellipsis. */
.calendar__pieces li {
  display: grid;
  justify-items: start;
  gap: 2px;
  min-width: 0;
}

.calendar__pieces .piece {
  width: 100%;
}

.calendar__pieces .badge {
  font-size: var(--text-2xs);
  padding: 0 6px;
}

/* In the legend the format chips sit inline with the text. */
.form-hint .piece {
  display: inline-block;
  vertical-align: middle;
  margin-inline: 2px;
}

/* RF-11: pending closure items stand out, with their state also in text. */
.row--pending td:first-child {
  box-shadow: inset 3px 0 0 var(--color-warning);
  padding-left: 12px;
}

/* ------------------------------------------------------------------------------------------
 * Dashboard (RF-13): a KPI row, then the blocks in a main column beside the month summary.
 * ---------------------------------------------------------------------------------------- */
/* The KPI row staggers its own cards, so the row itself does not slide in. */
.page > .stagger {
  animation: none;
}

/* Two cards per row on phones, then as many as fit (four on a laptop). */
.kpi-grid {
  display: grid;
  gap: var(--space-3);
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-bottom: var(--gutter);
}

@media (min-width: 720px) {
  .kpi-grid {
    grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr));
  }
}

.kpi {
  --tone: var(--accent-text);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) var(--gutter);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-sm);
  color: var(--fg);
  text-decoration: none;
}

.motion-ready .kpi {
  transition: box-shadow var(--duration-slow) var(--ease), border-color var(--duration-slow) var(--ease),
    transform var(--duration-slow) var(--ease);
}

.kpi:hover {
  box-shadow: var(--shadow-md);
  border-color: color-mix(in srgb, var(--tone) 30%, var(--border));
  transform: translateY(-2px);
}

.kpi__body {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.kpi__label {
  color: var(--fg-muted);
  font-size: var(--text-sm);
  font-weight: 500;
}

.kpi__value {
  font-family: var(--font-heading);
  font-size: 1.875rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.025em;
  font-variant-numeric: tabular-nums;
}

.kpi__hint {
  color: var(--fg-muted);
  font-size: var(--text-xs);
}

.kpi__icon {
  display: grid;
  flex: none;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-card);
  background: color-mix(in srgb, var(--tone) 12%, transparent);
  color: var(--tone);
}

.motion-ready .kpi__icon {
  transition: transform var(--duration-slow) var(--ease);
}

.kpi:hover .kpi__icon {
  transform: scale(1.1);
}

/* Phones: the icon tile sits in the corner so the label gets the full card width. */
@media (max-width: 719.98px) {
  .kpi {
    position: relative;
    padding: var(--space-3);
  }

  .kpi__label {
    padding-right: 36px;
  }

  .kpi__icon {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    border-radius: 10px;
  }
}

.dashboard-layout {
  display: grid;
  gap: var(--space-3);
  align-items: start;
}

/* Blocks flow in columns, each as tall as its content, so a short block leaves no hole. */
.dashboard-main {
  columns: 2 20rem;
  column-gap: var(--space-3);
}

.dashboard-main > .card {
  break-inside: avoid;
  margin-bottom: var(--space-3);
}

.dashboard-main > .card:last-child {
  margin-bottom: 0;
}

@media (min-width: 1200px) {
  .dashboard-layout {
    grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  }

  .dashboard-side {
    position: sticky;
    top: calc(var(--header-height) + var(--gutter));
  }
}

/* Block items: client first, detail below, state badge on the right. */
.feed {
  margin: 0;
  padding: 0;
  list-style: none;
}

.feed__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-inline: calc(var(--space-2) * -1);
  padding: 10px var(--space-2);
  border-radius: var(--radius-sm);
}

.motion-ready .feed__item {
  transition: background-color var(--duration-fast) var(--ease);
}

.feed__item:hover {
  background: var(--hover);
}

.feed__item + .feed__item {
  border-top: 1px solid var(--border);
}

.feed__text {
  display: grid;
  min-width: 0;
}

.feed__client {
  font-weight: 600;
  text-decoration: none;
}

.feed__client:hover {
  text-decoration: underline;
}

.feed__detail {
  color: var(--fg-muted);
  font-size: var(--text-sm);
}

.empty {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin: 0;
  color: var(--fg-muted);
  font-size: var(--text-sm);
}

.empty .icon {
  color: var(--color-success);
}

/* Summary figures: label on the left, value on the right. */
.stat-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.stat-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 6px 0;
  font-size: var(--text-sm);
}

.stat-list li + li {
  border-top: 1px dashed var(--border);
}

.stat-list strong {
  font-variant-numeric: tabular-nums;
}

#summary h3:first-of-type {
  margin-top: 0;
}

/* ------------------------------------------------------------------------------------------
 * Signed-out pages (login, TOTP setup): a centered card under the brand.
 * ---------------------------------------------------------------------------------------- */
.auth {
  min-height: 100vh;
  background:
    radial-gradient(60rem 40rem at 10% -10%, color-mix(in srgb, var(--color-accent) 14%, transparent), transparent 60%),
    radial-gradient(50rem 40rem at 110% 110%, color-mix(in srgb, var(--color-accent-soft) 18%, transparent), transparent 60%),
    var(--bg);
}

.auth__wrap {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: var(--gutter);
  min-height: 100vh;
  padding: var(--gutter) var(--space-3);
}

.auth .page {
  max-width: 28rem;
  padding: 0;
}

.brand--auth {
  color: var(--fg);
  font-size: var(--text-md);
}

/* On the light page background the soft accent is too pale for the brand suffix. */
.brand--auth .brand__suffix {
  color: var(--accent-text);
}

.auth-card {
  padding: 32px;
  box-shadow: var(--shadow-lg);
}

.auth-card__header {
  display: grid;
  justify-items: center;
  gap: var(--space-2);
  margin-bottom: var(--gutter);
  text-align: center;
}

.auth-card__header h1 {
  margin: 0;
  font-size: var(--text-lg);
}

.auth-card__icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: var(--radius-card);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent-text);
}

.auth-card__icon .icon {
  width: 22px;
  height: 22px;
}

.auth-card__subtitle {
  margin: 0;
  color: var(--fg-muted);
  font-size: var(--text-sm);
}

.auth-card__secondary {
  margin-top: var(--space-2);
}

.button--block {
  width: 100%;
  justify-content: center;
}

.form .button--block {
  justify-self: stretch;
  margin-top: var(--space-2);
}

@media (max-width: 479.98px) {
  .auth-card {
    padding: var(--gutter) 20px;
  }
}

.auth .card--narrow {
  box-shadow: var(--shadow-lg);
}

/* Resource bank (RF-33): a responsive grid of tiles; images show a thumbnail. */
.resource-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
  gap: var(--space-3);
  margin: 0;
  padding: 0;
  list-style: none;
}

.resource-tile {
  display: grid;
  align-content: start;
  gap: var(--space-2);
  min-width: 0;
  padding: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-input);
  background: var(--card);
}

.resource-tile__thumb {
  display: grid;
  place-items: center;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border-radius: var(--radius-sm);
  background: var(--hover);
  color: var(--fg-muted);
  font-size: var(--text-xs);
}

.resource-tile__thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.resource-tile__title {
  overflow-wrap: anywhere;
  font-weight: 600;
}

.resource-tile__tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

/* ------------------------------------------------------------------------------------------
 * Reorganized layout: page header with actions, tabs, card grids that size to their content
 * and forms no wider than their fields need.
 * ---------------------------------------------------------------------------------------- */
.page-header__row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.page-header__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  margin: 0;
}

/* Tabs: links to the panels of the page. Without JavaScript every panel stays visible and the
 * links jump to them; app.js turns them into ARIA tabs (`data-tab-default` marks the tab that
 * opens when nothing else picks one). */
.tabs {
  display: flex;
  gap: var(--space-1);
  margin: 0 0 var(--gutter);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: thin;
}

.tabs__tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  margin-bottom: -1px;
  padding: 10px 14px;
  border-bottom: 2px solid transparent;
  color: var(--fg-muted);
  font-size: var(--text-sm);
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
}

.motion-ready .tabs__tab {
  transition: color var(--duration-fast) var(--ease), border-color var(--duration-fast) var(--ease);
}

.tabs__tab:hover {
  color: var(--fg);
}

.tabs__tab[aria-selected="true"],
.tabs__tab[aria-current="page"] {
  border-bottom-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.tabs__count {
  min-width: 20px;
  padding: 0 6px;
  border-radius: var(--radius-full);
  background: var(--muted);
  color: var(--fg-muted);
  font-size: var(--text-2xs);
  line-height: 18px;
  text-align: center;
}

/* Second tier inside a page that already has section tabs: a segmented control. */
.tabs--segmented {
  width: fit-content;
  max-width: 100%;
  padding: 4px;
  border: 0;
  border-radius: var(--radius-button);
  background: var(--muted);
}

.tabs--segmented .tabs__tab {
  min-height: 36px;
  margin: 0;
  padding: 6px 14px;
  border: 0;
  border-radius: var(--radius-sm);
}

.tabs--segmented .tabs__tab[aria-selected="true"] {
  background: var(--card);
  box-shadow: var(--shadow-xs);
}

.tab-panel {
  display: grid;
  gap: var(--space-3);
  align-content: start;
}

.tab-panel + .tab-panel {
  margin-top: var(--space-3);
}

.tab-panel[hidden] {
  display: none;
}

/* Cards side by side, each as tall as its content. */
.card-grid {
  display: grid;
  gap: var(--space-3);
  align-items: start;
}

@media (min-width: 1024px) {
  .card-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card-grid--wide-first {
    grid-template-columns: minmax(0, 2fr) minmax(18rem, 1fr);
  }

  .card-grid > .card--full {
    grid-column: 1 / -1;
  }
}

/* A card title with its action on the right. */
.card__header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

/* In a table card the header keeps the card padding while the table runs edge to edge. */
.card--table > .card__header {
  margin: 0;
  padding: var(--gutter) var(--gutter) var(--space-3);
}

.card__header > h2,
.card__header > h3 {
  margin: 0;
}

/* A card that holds a form on its own row keeps a readable width instead of stretching its
 * fields across the page; cards in grids and inline row forms are unaffected. */
:is(.page, .tab-panel) > .card:has(> .form:not(.form--inline)) {
  max-width: 48rem;
}

/* Two fields per row where a form groups short values. */
@media (min-width: 720px) {
  .form-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-2) var(--space-3);
  }
}

.form-row > div {
  display: grid;
  gap: var(--space-2);
  align-content: start;
}

.card--table > .card__header + p {
  padding-top: 0;
  padding-bottom: var(--gutter);
}

/* A column of cards inside a card grid. */
.stack {
  display: grid;
  gap: var(--space-3);
  align-content: start;
  min-width: 0;
}

.table .table__nowrap {
  white-space: nowrap;
}

/* Monthly reports (RF-35): the sections of a report, on the sheet and in the print view. */
.report-section + .report-section {
  margin-top: var(--space-6, 1.5rem);
}

.report-block {
  margin-top: var(--space-4, 1rem);
}

.report-metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  gap: 0.75rem 1.5rem;
  margin: 0;
}

.report-metrics dt {
  color: var(--text-muted, inherit);
  font-size: 0.875rem;
}

.report-metrics dd {
  margin: 0;
  font-weight: 600;
}
