/* Pinova portal design system: the components. Every rule reads semantic
   tokens (base.css) and never a raw color or size, so a theme restyles all
   of it by setting tokens. Class names are flat: a block (.btn), its parts
   (.card-title) and its variants (.btn-primary). */

@layer components {

  /* ------------------------------------------------------------ document */

  body {
    background: var(--canvas);
    color: var(--fg);
    font-family: var(--font-sans);
    font-size: var(--text-body);
    line-height: var(--leading);
    font-feature-settings: "cv11", "ss01";
  }

  ::selection { background: var(--accent-soft); color: var(--on-accent-soft); }

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

  /* ---------------------------------------------------------- typography */

  .display {
    font-family: var(--font-display);
    font-size: clamp(var(--text-3xl), 4vw, var(--text-5xl));
    font-weight: var(--weight-heading);
    letter-spacing: var(--tracking-display);
    line-height: var(--leading-none);
  }
  h1, .h1 { font-family: var(--font-display); font-size: var(--text-2xl); font-weight: var(--weight-heading); letter-spacing: var(--tracking-heading); line-height: var(--leading-tight); }
  h2, .h2 { font-family: var(--font-display); font-size: var(--text-xl); font-weight: var(--weight-heading); letter-spacing: var(--tracking-heading); line-height: var(--leading-tight); }
  h3, .h3 { font-family: var(--font-display); font-size: var(--text-lg); font-weight: var(--weight-heading); letter-spacing: var(--tracking-heading); line-height: var(--leading-snug); }
  h4, .h4 { font-size: var(--text-md); font-weight: var(--weight-strong); line-height: var(--leading-snug); }

  .eyebrow {
    font-size: var(--text-2xs);
    font-weight: var(--weight-strong);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
    color: var(--fg-muted);
  }
  .lead { font-size: var(--text-lg); color: var(--fg-muted); line-height: var(--leading-relaxed); max-width: var(--measure); }
  .small { font-size: var(--text-sm); }
  .caption { font-size: var(--text-xs); color: var(--fg-muted); }

  .link {
    color: var(--link);
    text-decoration: underline;
    text-decoration-color: color-mix(in oklab, currentColor 35%, transparent);
    text-underline-offset: 0.2em;
    transition: text-decoration-color var(--dur-fast) var(--ease);
    &:hover { text-decoration-color: currentColor; }
  }

  /* A link that reads as text until hovered: names in tables and lists. */
  .link-quiet {
    text-decoration: underline;
    text-decoration-color: transparent;
    text-underline-offset: 0.2em;
    transition: text-decoration-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
    &:hover { color: var(--link); text-decoration-color: currentColor; }
  }

  code, .code {
    font-family: var(--font-mono);
    font-size: 0.9em;
    padding: 0.1em 0.35em;
    border-radius: var(--radius-xs);
    background: var(--surface-muted);
    border: 1px solid var(--border-subtle);
  }

  .kbd {
    display: inline-grid;
    place-items: center;
    min-width: 1.5em;
    height: 1.5em;
    padding: 0 0.35em;
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    font-weight: var(--weight-medium);
    line-height: 1;
    color: var(--fg-muted);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-xs);
    box-shadow: inset 0 -1px 0 var(--border-strong);
  }

  .prose {
    max-width: var(--measure);
    line-height: var(--leading-relaxed);
    & > * + * { margin-top: var(--space-3); }
    & h3 { margin-top: var(--space-6); }
    & ul { list-style: disc; padding-left: 1.25em; }
    & li + li { margin-top: var(--space-1); }
  }

  .divider { border: 0; border-top: 1px solid var(--border); margin: 0; }
  .divider-label {
    display: flex; align-items: center; gap: var(--space-3);
    font-size: var(--text-xs); color: var(--fg-subtle);
    &::before, &::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
  }

  /* -------------------------------------------------------------- button */

  .btn {
    --btn-bg: var(--surface);
    --btn-fg: var(--fg);
    --btn-border: var(--control-border);
    --btn-hover: var(--surface-hover);
    --btn-shadow: var(--control-shadow);
    --icon-size: 1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    height: var(--control-h);
    padding: 0 var(--control-px);
    font-size: var(--text-md);
    font-weight: var(--control-weight);
    line-height: 1;
    white-space: nowrap;
    color: var(--btn-fg);
    background: var(--btn-bg);
    border: 1px solid var(--btn-border);
    border-radius: var(--control-radius);
    box-shadow: var(--btn-shadow);
    cursor: pointer;
    user-select: none;
    transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
    &:hover { background: var(--btn-hover); }
    &:active { transform: translateY(0.5px); }
    &:disabled, &[aria-disabled="true"] { opacity: 0.5; pointer-events: none; box-shadow: none; }
    &[aria-busy="true"] {
      position: relative;
      color: transparent;
      pointer-events: none;
      &::after {
        content: "";
        position: absolute;
        inset: 0;
        margin: auto;
        width: 1em;
        height: 1em;
        border: 2px solid var(--btn-fg);
        border-right-color: transparent;
        border-radius: 50%;
        animation: ds-spin 0.7s linear infinite;
      }
    }
  }
  .btn-primary { --btn-bg: var(--accent); --btn-fg: var(--on-accent); --btn-border: var(--accent); --btn-hover: var(--accent-hover); }
  .btn-secondary { --btn-bg: var(--surface-muted); --btn-border: transparent; --btn-hover: var(--surface-hover); --btn-shadow: none; }
  .btn-outline { --btn-bg: transparent; --btn-shadow: none; }
  .btn-ghost { --btn-bg: transparent; --btn-border: transparent; --btn-shadow: none; --btn-fg: var(--fg-muted); --btn-hover: color-mix(in oklab, var(--fg) 7%, transparent); &:hover { color: var(--fg); } }
  .btn-danger { --btn-bg: var(--danger); --btn-fg: var(--on-danger); --btn-border: var(--danger); --btn-hover: var(--danger-hover); }
  .btn-danger-ghost { --btn-bg: transparent; --btn-border: transparent; --btn-shadow: none; --btn-fg: var(--danger); --btn-hover: var(--danger-soft); }
  .btn-link {
    --btn-bg: transparent; --btn-border: transparent; --btn-shadow: none; --btn-hover: transparent; --btn-fg: var(--link);
    height: auto; padding: 0;
    &:hover { text-decoration: underline; text-underline-offset: 0.2em; }
  }
  .btn-sm { height: var(--control-h-sm); padding: 0 calc(var(--control-px) * 0.75); font-size: var(--text-sm); gap: var(--space-1-5); --icon-size: 0.875rem; }
  .btn-lg { height: var(--control-h-lg); padding: 0 calc(var(--control-px) * 1.4); font-size: var(--text-base); --icon-size: 1.125rem; }
  .btn-icon { width: var(--control-h); padding: 0; }
  .btn-icon.btn-sm { width: var(--control-h-sm); }
  .btn-icon.btn-lg { width: var(--control-h-lg); }
  .btn-block { width: 100%; }

  .btn-group {
    display: inline-flex;
    & > .btn { border-radius: 0; box-shadow: none; }
    & > .btn + .btn { margin-left: -1px; }
    & > .btn:first-child { border-start-start-radius: var(--control-radius); border-end-start-radius: var(--control-radius); }
    & > .btn:last-child { border-start-end-radius: var(--control-radius); border-end-end-radius: var(--control-radius); }
    & > .btn:focus-visible { z-index: 1; }
  }

  @keyframes ds-spin { to { rotate: 1turn; } }

  /* --------------------------------------------------------------- badge */

  .badge {
    --badge-bg: var(--surface-muted);
    --badge-fg: var(--fg-muted);
    --badge-dot: var(--fg-subtle);
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    height: 1.375rem;
    padding: 0 var(--space-2);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    line-height: 1;
    white-space: nowrap;
    color: var(--badge-fg);
    background: var(--badge-bg);
    border-radius: var(--badge-radius);
    --icon-size: 0.75rem;
  }
  .badge-accent { --badge-bg: var(--accent-soft); --badge-fg: var(--on-accent-soft); --badge-dot: var(--accent); }
  .badge-success { --badge-bg: var(--success-soft); --badge-fg: var(--on-success-soft); --badge-dot: var(--success); }
  .badge-warning { --badge-bg: var(--warning-soft); --badge-fg: var(--on-warning-soft); --badge-dot: var(--warning); }
  .badge-danger { --badge-bg: var(--danger-soft); --badge-fg: var(--on-danger-soft); --badge-dot: var(--danger); }
  .badge-info { --badge-bg: var(--info-soft); --badge-fg: var(--on-info-soft); --badge-dot: var(--info); }
  .badge-outline { background: transparent; box-shadow: inset 0 0 0 1px var(--border-strong); }
  .badge-dot::before {
    content: "";
    width: 0.375rem;
    height: 0.375rem;
    border-radius: 50%;
    background: var(--badge-dot);
  }

  .count {
    display: inline-grid;
    place-items: center;
    min-width: 1.25rem;
    height: 1.25rem;
    padding: 0 0.3rem;
    font-size: var(--text-2xs);
    font-weight: var(--weight-strong);
    font-variant-numeric: tabular-nums;
    color: var(--on-accent);
    background: var(--accent);
    border-radius: var(--radius-full);
  }

  .chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    height: 1.625rem;
    padding: 0 var(--space-1) 0 var(--space-2-5);
    font-size: var(--text-sm);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    --icon-size: 0.875rem;
    & button {
      display: grid;
      place-items: center;
      width: 1.125rem;
      height: 1.125rem;
      border-radius: 50%;
      color: var(--fg-subtle);
      &:hover { background: var(--surface-hover); color: var(--fg); }
    }
  }

  /* -------------------------------------------------------------- avatar */

  .avatar {
    --size: 2rem;
    display: inline-grid;
    place-items: center;
    flex-shrink: 0;
    width: var(--size);
    height: var(--size);
    font-size: calc(var(--size) * 0.38);
    font-weight: var(--weight-strong);
    color: var(--on-accent-soft);
    background: var(--accent-soft);
    border-radius: var(--avatar-radius, 50%);
    letter-spacing: 0.02em;
  }
  .avatar-sm { --size: 1.5rem; }
  .avatar-lg { --size: 2.75rem; }
  .avatar-stack {
    display: inline-flex;
    & .avatar { box-shadow: 0 0 0 2px var(--surface); }
    & .avatar + .avatar { margin-left: -0.3rem; }
  }

  /* --------------------------------------------------------------- forms */

  .field {
    display: grid;
    gap: var(--space-1-5);
    align-content: start;
  }
  .label {
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--fg);
    & .optional { font-weight: var(--weight-normal); color: var(--fg-subtle); }
  }
  .hint { font-size: var(--text-xs); color: var(--fg-muted); }
  .error-text {
    display: flex; align-items: center; gap: var(--space-1);
    font-size: var(--text-xs); color: var(--danger);
    --icon-size: 0.875rem;
  }

  .input, .select, .textarea {
    width: 100%;
    min-width: 0;
    height: var(--control-h);
    padding: 0 var(--control-px);
    font-size: var(--text-md);
    color: var(--fg);
    background: var(--surface-inset);
    border: 1px solid var(--control-border);
    border-radius: var(--control-radius);
    box-shadow: var(--control-shadow);
    transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
    &::placeholder { color: var(--fg-subtle); }
    &:hover:not(:disabled, :focus) { border-color: color-mix(in oklab, var(--control-border), var(--fg) 18%); }
    &:focus-visible { outline: var(--focus-width) solid var(--focus); outline-offset: -1px; border-color: transparent; }
    &:disabled { background: var(--surface-muted); color: var(--fg-subtle); cursor: not-allowed; box-shadow: none; }
    &[aria-invalid="true"] {
      border-color: var(--danger);
      &:focus-visible { outline-color: var(--danger); }
    }
    &[readonly] { background: var(--surface-muted); }
  }
  .input-sm { height: var(--control-h-sm); font-size: var(--text-sm); }
  .input[type="number"], .input.num { font-variant-numeric: tabular-nums; }
  .input[type="date"] { font-variant-numeric: tabular-nums; }
  .textarea {
    height: auto;
    min-height: 5.5rem;
    padding: var(--space-2) var(--control-px);
    line-height: var(--leading);
    resize: vertical;
    field-sizing: content;
  }
  .select {
    appearance: none;
    padding-right: calc(var(--control-px) + 1.25rem);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23888'%3E%3Cpath d='M12 13.17l4.95-4.95 1.41 1.41L12 16l-6.36-6.37 1.41-1.41z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right calc(var(--control-px) - 0.25rem) center;
    background-size: 1.125rem;
    cursor: pointer;
  }

  /* An input with a leading or trailing icon (the icon is a sibling). */
  .input-icon {
    position: relative;
    display: block;
    --icon-size: 1rem;
    & > svg {
      position: absolute;
      top: 50%;
      left: var(--control-px);
      translate: 0 -50%;
      color: var(--fg-subtle);
      pointer-events: none;
    }
    & > .input { padding-left: calc(var(--control-px) + 1.5rem); }
    & > .kbd { position: absolute; right: var(--space-2); top: 50%; translate: 0 -50%; }
  }

  /* Attached addons: "$" before an amount, "hrs" after a quantity. */
  .input-group {
    display: flex;
    & > .input { border-radius: 0; flex: 1; }
    & > :first-child { border-start-start-radius: var(--control-radius); border-end-start-radius: var(--control-radius); }
    & > :last-child { border-start-end-radius: var(--control-radius); border-end-end-radius: var(--control-radius); }
    & > * + * { margin-left: -1px; }
    & > .input:focus-visible { z-index: 1; }
  }
  .addon {
    display: inline-flex;
    align-items: center;
    padding: 0 var(--space-3);
    font-size: var(--text-sm);
    color: var(--fg-muted);
    background: var(--surface-muted);
    border: 1px solid var(--control-border);
    white-space: nowrap;
  }

  /* Checkbox and radio: native inputs, restyled; the label wraps them. */
  .check {
    display: inline-flex;
    align-items: flex-start;
    gap: var(--space-2);
    font-size: var(--text-md);
    cursor: pointer;
    & .hint { display: block; margin-top: var(--space-0-5); }
    &:has(:disabled) { opacity: 0.55; cursor: not-allowed; }
  }
  .checkbox, .radio {
    appearance: none;
    flex-shrink: 0;
    display: grid;
    place-content: center;
    width: 1rem;
    height: 1rem;
    margin-top: 0.15em;
    background: var(--surface-inset);
    border: 1px solid var(--control-border);
    box-shadow: var(--control-shadow);
    cursor: inherit;
    transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
    &::before { content: ""; transform: scale(0); transition: transform var(--dur-fast) var(--ease-out); }
    &:checked { background: var(--accent); border-color: var(--accent); }
    &:checked::before { transform: scale(1); }
    &:focus-visible { outline: var(--focus-width) solid var(--focus); outline-offset: var(--focus-offset); }
  }
  .checkbox {
    border-radius: var(--checkbox-radius, var(--radius-xs));
    &::before {
      width: 0.625rem;
      height: 0.625rem;
      background: var(--on-accent);
      clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
    }
    &:indeterminate { background: var(--accent); border-color: var(--accent); }
    &:indeterminate::before { transform: scale(1); clip-path: inset(40% 0); }
  }
  .radio {
    border-radius: 50%;
    &::before { width: 0.375rem; height: 0.375rem; border-radius: 50%; background: var(--on-accent); }
  }

  .switch {
    appearance: none;
    position: relative;
    flex-shrink: 0;
    width: 2rem;
    height: 1.125rem;
    margin-top: 0.1em;
    background: var(--border-strong);
    border-radius: var(--radius-full);
    cursor: inherit;
    transition: background-color var(--dur) var(--ease);
    &::before {
      content: "";
      position: absolute;
      top: 2px;
      left: 2px;
      width: calc(1.125rem - 4px);
      height: calc(1.125rem - 4px);
      background: oklch(1 0 0);
      border-radius: 50%;
      box-shadow: 0 1px 2px oklch(0 0 0 / 0.2);
      transition: translate var(--dur) var(--ease);
    }
    &:checked { background: var(--accent); }
    &:checked::before { translate: 0.875rem 0; }
  }

  /* Segmented control: radios in a track (view toggles, small filters). */
  .segmented {
    display: inline-flex;
    flex-wrap: wrap;
    max-width: 100%;
    padding: 2px;
    gap: 2px;
    background: var(--surface-muted);
    border: 1px solid var(--border);
    border-radius: calc(var(--control-radius) + 2px);
    & label {
      display: inline-flex;
      align-items: center;
      gap: var(--space-1-5);
      height: calc(var(--control-h) - 6px);
      padding: 0 var(--space-3);
      font-size: var(--text-sm);
      font-weight: var(--weight-medium);
      color: var(--fg-muted);
      border-radius: var(--control-radius);
      cursor: pointer;
      transition: color var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease);
      --icon-size: 0.875rem;
      &:hover { color: var(--fg); }
      &:has(:checked) { color: var(--fg); background: var(--surface); box-shadow: var(--shadow-sm), 0 0 0 1px var(--border); }
      &:has(:focus-visible) { outline: var(--focus-width) solid var(--focus); outline-offset: 1px; }
    }
    & input { position: absolute; opacity: 0; pointer-events: none; }
  }

  .dropzone {
    display: grid;
    justify-items: center;
    gap: var(--space-2);
    padding: var(--space-8) var(--space-6);
    text-align: center;
    color: var(--fg-muted);
    font-size: var(--text-sm);
    background: var(--surface-muted);
    border: 1.5px dashed var(--border-strong);
    border-radius: var(--card-radius);
    cursor: pointer;
    transition: border-color var(--dur) var(--ease), background-color var(--dur) var(--ease);
    --icon-size: 1.5rem;
    & strong { color: var(--fg); font-weight: var(--weight-medium); }
    &:hover { border-color: var(--accent); background: var(--accent-soft); }
  }

  .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
    gap: var(--space-4) var(--space-5);
    & .span-all { grid-column: 1 / -1; }
  }
  .form-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-2);
  }
  .fieldset {
    display: grid;
    gap: var(--space-3);
    & legend { padding: 0; font-size: var(--text-sm); font-weight: var(--weight-medium); margin-bottom: var(--space-2); }
  }

  /* ---------------------------------------------------------------- card */

  .card {
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--surface);
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
  }
  .card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    padding: var(--card-pad) var(--card-pad) 0;
  }
  .card-title { font-family: var(--font-display); font-size: var(--text-base); font-weight: var(--weight-heading); letter-spacing: var(--tracking-heading); line-height: var(--leading-snug); }
  .card-desc { margin-top: var(--space-0-5); font-size: var(--text-sm); color: var(--fg-muted); }
  .card-body { padding: var(--card-pad); flex: 1; }
  .card-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-3) var(--card-pad);
    border-top: 1px solid var(--border-subtle);
    font-size: var(--text-sm);
    color: var(--fg-muted);
  }
  .card-flush { padding: 0; }
  .card-header + .card-flush { margin-top: var(--space-4); }
  .card-interactive {
    transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
    &:hover { border-color: var(--border-strong); box-shadow: var(--shadow-md); }
  }

  /* ---------------------------------------------------------------- stat */

  .stat {
    display: grid;
    align-content: start;
    gap: var(--space-1);
    min-width: 0;
  }
  .stat-label {
    display: flex; align-items: center; gap: var(--space-1-5);
    font-size: var(--text-sm); color: var(--fg-muted);
    --icon-size: 0.9375rem;
  }
  .stat-value {
    font-family: var(--font-figures);
    font-size: var(--text-2xl);
    font-weight: var(--stat-weight, var(--weight-strong));
    letter-spacing: var(--tracking-heading);
    line-height: var(--leading-tight);
    font-variant-numeric: tabular-nums;
  }
  .stat-meta { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-xs); color: var(--fg-muted); }
  .delta {
    display: inline-flex; align-items: center; gap: 0.15rem;
    font-weight: var(--weight-medium);
    font-variant-numeric: tabular-nums;
    --icon-size: 0.75rem;
  }
  .delta-up { color: var(--on-success-soft); }
  .delta-down { color: var(--danger); }

  .stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr));
    background: var(--surface);
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
    overflow: hidden;
    & .stat { padding: var(--card-pad); box-shadow: -1px 0 0 var(--border-subtle), 0 -1px 0 var(--border-subtle); }
  }

  /* --------------------------------------------------------------- table */

  .table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
  }
  .card .table-wrap, .table-wrap.bare { border: 0; border-radius: 0; box-shadow: none; background: none; }

  .table {
    width: 100%;
    font-size: var(--text-md);
    & th, & td {
      padding: var(--cell-py) var(--cell-px);
      text-align: left;
      vertical-align: middle;
      white-space: nowrap;
    }
    & :is(th, td):first-child { padding-left: calc(var(--cell-px) + var(--space-1)); }
    & :is(th, td):last-child { padding-right: calc(var(--cell-px) + var(--space-1)); }
    & thead th {
      position: sticky;
      top: 0;
      z-index: 1;
      font-size: var(--th-size);
      font-weight: var(--th-weight);
      color: var(--th-fg);
      text-transform: var(--th-transform);
      letter-spacing: var(--th-tracking);
      background: var(--th-bg);
      box-shadow: inset 0 -1px 0 var(--border);
    }
    & tbody tr {
      border-top: 1px solid var(--row-border);
      transition: background-color var(--dur-fast) var(--ease);
    }
    & tbody tr:first-child { border-top: 0; }
    & tbody tr:hover { background: var(--row-hover); }
    & tbody tr[aria-selected="true"] { background: var(--accent-soft); }
    & tfoot td {
      font-weight: var(--weight-strong);
      border-top: var(--total-rule);
    }
    & .num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--font-figures); }
    & .shrink { width: 1%; }
    & .wrap { white-space: normal; }
    & .muted { color: var(--fg-muted); }
    & .row-total td { font-weight: var(--weight-strong); border-top: var(--total-rule); }
    & .row-total:hover { background: none; }
    & .row-heading td { padding-top: var(--space-4); font-size: var(--text-xs); font-weight: var(--weight-strong); color: var(--fg-muted); text-transform: uppercase; letter-spacing: var(--tracking-caps); }
    & .row-heading:hover { background: none; }
    & .table-actions { display: flex; justify-content: flex-end; gap: var(--space-1); }
    & .row-actions { opacity: 0; transition: opacity var(--dur-fast) var(--ease); }
    & tr:hover .row-actions, & tr:focus-within .row-actions { opacity: 1; }
    & .check { vertical-align: middle; }
  }
  .table-dense { --cell-py: calc(var(--cell-py-dense, 0.3125rem)); font-size: var(--text-sm); }

  .sort {
    display: inline-flex; align-items: center; gap: var(--space-1);
    font: inherit; color: inherit; letter-spacing: inherit; text-transform: inherit;
    cursor: pointer;
    --icon-size: 0.75rem;
    & svg { opacity: 0.4; }
    &:hover { color: var(--fg); & svg { opacity: 1; } }
    &[aria-sort] { color: var(--fg); & svg { opacity: 1; } }
    &[aria-sort="ascending"] svg { rotate: 180deg; }
  }

  .cell-primary { font-weight: var(--weight-medium); color: var(--fg); }
  .cell-secondary { font-size: var(--text-xs); color: var(--fg-muted); }
  .cell-person { display: flex; align-items: center; gap: var(--space-2-5); }

  .toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    & .grow { flex: 1; min-width: 12rem; }
    & > .select, & > .input { width: auto; }
  }

  /* A row of labelled filter fields with their Apply button: a report's
     period, a list's filters. */
  .filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: var(--space-3);
    & .field { min-width: 10rem; }
    & .field-grow { flex: 1; min-width: 14rem; }
  }

  /* ---------------------------------------------------- description list */

  .dl {
    display: grid;
    grid-template-columns: minmax(8rem, max-content) 1fr;
    gap: var(--space-2-5) var(--space-6);
    font-size: var(--text-md);
    & dt { color: var(--fg-muted); }
    & dd { margin: 0; min-width: 0; }
  }

  /* ---------------------------------------------------------- list group */

  .list {
    & > li {
      display: flex;
      align-items: center;
      gap: var(--space-3);
      padding: var(--space-3) var(--card-pad);
      border-top: 1px solid var(--border-subtle);
    }
    & > li:first-child { border-top: 0; }
    & .list-main { flex: 1; min-width: 0; }
    & .list-title { font-weight: var(--weight-medium); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    & .list-sub { font-size: var(--text-xs); color: var(--fg-muted); }
  }

  /* --------------------------------------------------------------- alert */

  .alert {
    --alert-bg: var(--surface-muted);
    --alert-fg: var(--fg);
    --alert-icon: var(--fg-muted);
    --alert-border: var(--border);
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: start;
    gap: var(--space-1) var(--space-3);
    padding: var(--space-3) var(--space-4);
    font-size: var(--text-md);
    color: var(--alert-fg);
    background: var(--alert-bg);
    border: 1px solid var(--alert-border);
    border-radius: var(--card-radius);
    --icon-size: 1.125rem;
    & > svg { color: var(--alert-icon); margin-top: 0.1em; }
    & .alert-title { font-weight: var(--weight-strong); }
    & .alert-body { color: var(--alert-fg); }
    & .alert-actions { grid-column: 2; display: flex; gap: var(--space-2); margin-top: var(--space-2); }
    & > :only-child { grid-column: 1 / -1; }
    & > .btn-ghost { --btn-fg: var(--alert-fg); --btn-hover: color-mix(in oklab, var(--alert-fg) 10%, transparent); margin: calc(var(--space-1) * -1) calc(var(--space-2) * -1) 0 0; &:hover { color: var(--alert-fg); } }
  }
  .alert-info { --alert-bg: var(--info-soft); --alert-fg: var(--on-info-soft); --alert-icon: var(--info); --alert-border: color-mix(in oklab, var(--info) 22%, transparent); }
  .alert-success { --alert-bg: var(--success-soft); --alert-fg: var(--on-success-soft); --alert-icon: var(--success); --alert-border: color-mix(in oklab, var(--success) 22%, transparent); }
  .alert-warning { --alert-bg: var(--warning-soft); --alert-fg: var(--on-warning-soft); --alert-icon: var(--warning); --alert-border: color-mix(in oklab, var(--warning) 30%, transparent); }
  .alert-danger { --alert-bg: var(--danger-soft); --alert-fg: var(--on-danger-soft); --alert-icon: var(--danger); --alert-border: color-mix(in oklab, var(--danger) 22%, transparent); }

  /* The page-wide notice: one line across the top of a page. */
  .banner {
    display: flex; align-items: center; gap: var(--space-3);
    padding: var(--space-2) var(--space-4);
    font-size: var(--text-sm);
    color: var(--on-accent);
    background: var(--accent);
    border-radius: var(--control-radius);
    --icon-size: 1rem;
    & a { text-decoration: underline; text-underline-offset: 0.2em; margin-left: auto; font-weight: var(--weight-medium); }
  }

  /* --------------------------------------------------------------- toast */

  .toast-region {
    position: fixed;
    right: var(--space-4);
    bottom: var(--space-4);
    z-index: var(--z-toast);
    display: grid;
    gap: var(--space-2);
    width: min(24rem, calc(100vw - 2rem));
  }
  .toast {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
    font-size: var(--text-md);
    background: var(--surface-raised);
    border: 1px solid var(--border);
    border-radius: var(--card-radius);
    box-shadow: var(--shadow-lg);
    --icon-size: 1.125rem;
    & > svg { margin-top: 0.1em; }
    & .toast-title { font-weight: var(--weight-medium); }
    & .toast-body { font-size: var(--text-sm); color: var(--fg-muted); }
    &.toast-success > svg { color: var(--success); }
    &.toast-danger > svg { color: var(--danger); }
    &.toast-info > svg { color: var(--info); }
    &[data-enter] { animation: ds-toast-in var(--dur-slow) var(--ease-out); }
    &[data-leave] { animation: ds-toast-out var(--dur) var(--ease) forwards; }
  }
  @keyframes ds-toast-in { from { opacity: 0; translate: 0 0.75rem; scale: 0.97; } }
  @keyframes ds-toast-out { to { opacity: 0; translate: 0.75rem 0; } }

  /* ------------------------------------------------- progress and loading */

  .progress {
    appearance: none;
    display: block;
    width: 100%;
    height: 0.375rem;
    overflow: hidden;
    border: 0;
    border-radius: var(--radius-full);
    background: var(--surface-muted);
    box-shadow: inset 0 0 0 1px var(--border-subtle);
    color: var(--accent);
    &::-webkit-progress-bar { background: transparent; }
    &::-webkit-progress-value { background: currentColor; border-radius: inherit; transition: inline-size var(--dur-slow) var(--ease); }
    &::-moz-progress-bar { background: currentColor; border-radius: inherit; }
  }
  .progress-success { color: var(--success); }
  .progress-warning { color: var(--warning); }
  .progress-danger { color: var(--danger); }

  .spinner {
    display: inline-block;
    width: var(--size, 1.125rem);
    height: var(--size, 1.125rem);
    border: 2px solid var(--border-strong);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: ds-spin 0.7s linear infinite;
  }

  .skeleton {
    display: block;
    height: var(--h, 0.875rem);
    width: var(--w, 100%);
    border-radius: var(--radius-sm);
    background: linear-gradient(90deg, var(--surface-muted) 0%, var(--surface-hover) 50%, var(--surface-muted) 100%);
    background-size: 200% 100%;
    animation: ds-shimmer 1.4s var(--ease) infinite;
  }
  @keyframes ds-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

  /* ---------------------------------------------------------------- tabs */

  .tabs {
    display: flex;
    gap: var(--space-5);
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    scrollbar-width: none;
  }
  .tab {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    padding: var(--space-2) 0 var(--space-2-5);
    font-size: var(--text-md);
    font-weight: var(--weight-medium);
    color: var(--fg-muted);
    white-space: nowrap;
    transition: color var(--dur-fast) var(--ease);
    &:hover { color: var(--fg); }
    &[aria-selected="true"], &[aria-current="page"] {
      color: var(--fg);
      &::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: var(--tab-indicator, var(--accent)); border-radius: 2px 2px 0 0; }
    }
    & .count { background: var(--surface-muted); color: var(--fg-muted); }
    &[aria-selected="true"] .count { background: var(--accent-soft); color: var(--on-accent-soft); }
  }
  .tab-panel { padding-top: var(--space-4); }

  .pills {
    display: inline-flex;
    flex-wrap: wrap;
    gap: var(--space-1);
    & a, & button {
      display: inline-flex; align-items: center; gap: var(--space-1-5);
      height: var(--control-h-sm);
      padding: 0 var(--space-3);
      font-size: var(--text-sm);
      font-weight: var(--weight-medium);
      color: var(--fg-muted);
      border-radius: var(--radius-full);
      transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
      &:hover { color: var(--fg); background: var(--surface-muted); }
      &[aria-current="true"] { color: var(--on-accent); background: var(--accent); }
    }
  }

  /* ------------------------------------------------------- breadcrumbs */

  .breadcrumbs {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-1-5);
    font-size: var(--text-sm);
    color: var(--fg-muted);
    & a:hover { color: var(--fg); }
    & li { display: inline-flex; align-items: center; gap: var(--space-1-5); }
    & li + li::before { content: "/"; color: var(--fg-subtle); opacity: 0.7; }
    & [aria-current="page"] { color: var(--fg); font-weight: var(--weight-medium); }
  }

  /* ---------------------------------------------------------- pagination */

  .pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    font-size: var(--text-sm);
    color: var(--fg-muted);
    & .pages { display: flex; gap: var(--space-1); }
    & .page {
      display: inline-grid; place-items: center;
      min-width: var(--control-h-sm); height: var(--control-h-sm);
      padding: 0 var(--space-1-5);
      font-variant-numeric: tabular-nums;
      font-weight: var(--weight-medium);
      border-radius: var(--control-radius);
      --icon-size: 1rem;
      &:hover { background: var(--surface-hover); color: var(--fg); }
      &[aria-current="page"] { color: var(--fg); background: var(--surface); box-shadow: 0 0 0 1px var(--control-border), var(--control-shadow); }
    }
  }

  /* ---------------------------------------------------------- dropdown */

  .dropdown {
    position: relative;
    display: inline-block;
    & > summary { list-style: none; }
    &[open] > summary { background: var(--surface-hover); }
    &[open] > summary.btn { background: var(--btn-hover); }
  }
  .menu {
    position: absolute;
    top: calc(100% + var(--space-1));
    left: 0;
    z-index: var(--z-dropdown);
    min-width: 12rem;
    padding: var(--space-1);
    background: var(--surface-raised);
    border: 1px solid var(--border);
    border-radius: var(--card-radius);
    box-shadow: var(--shadow-md);
    animation: ds-pop var(--dur) var(--ease-out);
    transform-origin: top left;
    &.menu-end { left: auto; right: 0; transform-origin: top right; }
    & hr { border: 0; border-top: 1px solid var(--border-subtle); margin: var(--space-1) calc(var(--space-1) * -1); }
  }
  .menu-label { padding: var(--space-1-5) var(--space-2) var(--space-1); font-size: var(--text-xs); color: var(--fg-subtle); font-weight: var(--weight-medium); }
  .menu-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    padding: var(--space-1-5) var(--space-2);
    font-size: var(--text-md);
    text-align: left;
    color: var(--fg);
    border-radius: calc(var(--card-radius) - var(--space-1));
    cursor: pointer;
    --icon-size: 1rem;
    & svg { color: var(--fg-muted); }
    & .kbd { margin-left: auto; }
    &:hover, &:focus-visible { background: var(--surface-hover); outline: none; }
    &.menu-item-danger { color: var(--danger); & svg { color: currentColor; } &:hover { background: var(--danger-soft); } }
  }
  @keyframes ds-pop { from { opacity: 0; scale: 0.97; translate: 0 -2px; } }

  /* Tooltip: CSS only, from data-tip on any element. */
  [data-tip] {
    position: relative;
    &::after {
      content: attr(data-tip);
      position: absolute;
      bottom: calc(100% + 6px);
      left: 50%;
      z-index: var(--z-dropdown);
      translate: -50% 2px;
      padding: var(--space-1) var(--space-2);
      font-size: var(--text-xs);
      font-weight: var(--weight-medium);
      line-height: var(--leading-snug);
      white-space: nowrap;
      color: var(--tip-fg, var(--canvas));
      background: var(--tip-bg, var(--fg));
      border-radius: var(--radius-sm);
      opacity: 0;
      pointer-events: none;
      transition: opacity var(--dur-fast) var(--ease), translate var(--dur-fast) var(--ease);
    }
    &:hover::after, &:focus-visible::after { opacity: 1; translate: -50% 0; transition-delay: 300ms; }
  }

  /* -------------------------------------------------------------- dialog */

  .dialog {
    width: min(28rem, calc(100vw - 2rem));
    margin: auto;
    color: var(--fg);
    background: var(--surface-raised);
    border: 1px solid var(--border);
    border-radius: calc(var(--card-radius) + 4px);
    box-shadow: var(--shadow-lg);
    &::backdrop { background: var(--backdrop); backdrop-filter: blur(2px); }
    &[open] { animation: ds-dialog-in var(--dur-slow) var(--ease-out); }
    &[open]::backdrop { animation: ds-fade var(--dur-slow) var(--ease-out); }
    /* A dialog the server renders open (a form re-shown with its error)
       before portal.js reopens it as a modal. */
    &[open]:not(:modal) { position: fixed; inset: 0; z-index: var(--z-overlay); height: fit-content; }
  }
  .dialog-lg { width: min(40rem, calc(100vw - 2rem)); }
  .dialog-xl { width: min(48rem, calc(100vw - 2rem)); }
  .dialog-close { position: absolute; top: var(--space-3); right: var(--space-3); }
  .dialog:has(.dialog-close) .dialog-header { padding-right: var(--space-12); }
  .dialog-header { display: grid; gap: var(--space-1-5); padding: var(--space-6) var(--space-6) 0; }
  .dialog-title { font-family: var(--font-display); font-size: var(--text-lg); font-weight: var(--weight-heading); letter-spacing: var(--tracking-heading); }
  .dialog-desc { font-size: var(--text-md); color: var(--fg-muted); }
  .dialog-body { padding: var(--space-5) var(--space-6); }
  .dialog-footer {
    display: flex; justify-content: flex-end; gap: var(--space-2);
    padding: var(--space-4) var(--space-6);
    background: var(--dialog-footer-bg, transparent);
    border-top: 1px solid var(--dialog-footer-border, transparent);
    border-end-start-radius: inherit;
    border-end-end-radius: inherit;
  }
  .dialog-icon {
    display: grid; place-items: center;
    width: 2.5rem; height: 2.5rem;
    border-radius: 50%;
    color: var(--danger);
    background: var(--danger-soft);
    --icon-size: 1.25rem;
  }
  @keyframes ds-dialog-in { from { opacity: 0; scale: 0.96; translate: 0 6px; } }
  @keyframes ds-fade { from { opacity: 0; } }

  /* ----------------------------------------------------------- accordion */

  .accordion {
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    background: var(--surface);
    box-shadow: var(--card-shadow);
    & details + details { border-top: 1px solid var(--border-subtle); }
    & summary {
      display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
      padding: var(--space-3) var(--space-4);
      font-weight: var(--weight-medium);
      --icon-size: 1rem;
      & svg { color: var(--fg-subtle); transition: rotate var(--dur) var(--ease); }
      &:hover { background: var(--surface-muted); }
    }
    & details:first-child summary { border-radius: var(--card-radius) var(--card-radius) 0 0; }
    & details[open] summary svg { rotate: 90deg; }
    & .accordion-body { padding: 0 var(--space-4) var(--space-4); color: var(--fg-muted); font-size: var(--text-md); }
  }

  /* --------------------------------------------------------------- steps */

  .steps {
    display: flex;
    gap: var(--space-2);
    counter-reset: step;
    & li {
      flex: 1;
      display: grid;
      gap: var(--space-1-5);
      padding-top: var(--space-3);
      font-size: var(--text-sm);
      color: var(--fg-subtle);
      border-top: 2px solid var(--border);
      counter-increment: step;
      &::before { content: "Step " counter(step); font-size: var(--text-xs); font-weight: var(--weight-medium); }
      & strong { color: var(--fg-muted); font-weight: var(--weight-medium); }
    }
    & [data-state="done"] { border-color: var(--accent); & strong { color: var(--fg); } }
    & [data-state="current"] { border-color: var(--accent); color: var(--accent); & strong { color: var(--fg); } }
  }

  /* ------------------------------------------------------------ timeline */

  .timeline {
    display: grid;
    & li {
      position: relative;
      display: grid;
      grid-template-columns: 1.75rem 1fr auto;
      gap: var(--space-3);
      padding-bottom: var(--space-4);
      &::before {
        content: "";
        position: absolute;
        left: calc(0.875rem - 0.5px);
        top: 1.75rem;
        bottom: 0;
        border-left: 1px solid var(--border);
      }
      &:last-child { padding-bottom: 0; &::before { display: none; } }
    }
    & .timeline-icon {
      display: grid; place-items: center;
      width: 1.75rem; height: 1.75rem;
      border-radius: 50%;
      color: var(--fg-muted);
      background: var(--surface-muted);
      box-shadow: 0 0 0 1px var(--border);
      --icon-size: 0.875rem;
    }
    & .timeline-icon-accent { color: var(--on-accent); background: var(--accent); box-shadow: none; }
    & .timeline-icon-success { color: var(--on-success-soft); background: var(--success-soft); box-shadow: none; }
    & .timeline-text { font-size: var(--text-md); padding-top: 0.2rem; & strong { font-weight: var(--weight-medium); } }
    & .timeline-time { font-size: var(--text-xs); color: var(--fg-subtle); padding-top: 0.3rem; font-variant-numeric: tabular-nums; }
  }

  /* --------------------------------------------------------------- empty */

  .empty {
    display: grid;
    justify-items: center;
    gap: var(--space-2);
    padding: var(--space-12) var(--space-6);
    text-align: center;
    & .empty-icon {
      display: grid; place-items: center;
      width: 3rem; height: 3rem;
      margin-bottom: var(--space-2);
      color: var(--fg-muted);
      background: var(--surface-muted);
      border-radius: var(--card-radius);
      box-shadow: inset 0 0 0 1px var(--border);
      --icon-size: 1.375rem;
    }
    & .empty-title { font-weight: var(--weight-strong); font-size: var(--text-base); }
    & .empty-text { font-size: var(--text-md); color: var(--fg-muted); max-width: 34ch; }
    & .form-actions, & .cluster { margin-top: var(--space-3); justify-content: center; }
    & .alert { justify-self: stretch; text-align: start; }
  }

  /* ---------------------------------------------------------- app shell */

  /* The page frame: the sidebar beside the main column. The sidebar toggle
     (portal.js) sets data-sidebar on the shell: "collapsed" hides it on a
     wide screen; on a narrow one, where it starts hidden, "open" lays it
     over the page. */
  .shell {
    --sidebar-w: 15rem;
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    min-height: 100svh;
    background: var(--canvas);
    &[data-sidebar="collapsed"] { grid-template-columns: minmax(0, 1fr); & > .sidebar { display: none; } }
    @media (width < 760px) {
      grid-template-columns: minmax(0, 1fr);
      & > .sidebar { display: none; }
      &[data-sidebar="open"] > .sidebar {
        display: flex;
        position: fixed;
        inset: 0 auto 0 0;
        z-index: var(--z-overlay);
        z-index: calc(var(--z-overlay) + 1);
        width: min(var(--sidebar-w), calc(100vw - var(--space-12)));
        box-shadow: var(--shadow-lg);
      }
      &[data-sidebar="open"] > .shell-scrim { display: block; }
    }
  }
  .shell-main { display: flex; flex-direction: column; min-width: 0; }
  .shell-scrim {
    display: none;
    position: fixed;
    inset: 0;
    z-index: var(--z-overlay);
    background: var(--backdrop);
    animation: ds-fade var(--dur) var(--ease-out);
  }
  .sidebar {
    position: sticky;
    top: 0;
    height: 100svh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    padding: var(--space-3);
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
  }
  .sidebar-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    margin-top: auto;
    padding: var(--space-3) var(--space-1) 0;
    border-top: 1px solid var(--border-subtle);
  }
  .brand {
    display: flex; align-items: center; gap: var(--space-2);
    padding: var(--space-1) var(--space-2);
    font-weight: var(--weight-strong);
    font-size: var(--text-md);
  }
  .brand-mark {
    display: grid; place-items: center;
    width: 1.5rem; height: 1.5rem;
    color: var(--on-accent);
    background: var(--accent);
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-family: var(--font-display);
    font-weight: var(--weight-strong);
  }
  .nav {
    display: grid;
    gap: 1px;
    & .nav-label { padding: var(--space-3) var(--space-2) var(--space-1); }
  }
  .nav-item {
    display: flex;
    align-items: center;
    gap: var(--space-2-5);
    height: 2rem;
    padding: 0 var(--space-2);
    font-size: var(--text-md);
    color: var(--fg-muted);
    border-radius: var(--control-radius);
    transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
    --icon-size: 1rem;
    & > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    & .count { margin-left: auto; background: var(--surface-hover); color: var(--fg-muted); }
    &:hover { background: var(--surface-hover); color: var(--fg); }
    &[aria-current="page"] {
      color: var(--nav-active-fg);
      background: var(--nav-active-bg);
      font-weight: var(--weight-medium);
      & svg { color: var(--nav-active-icon, currentColor); }
    }
  }
  /* A nav entry with sub-pages (Reports, Finances): a <details> whose
     summary is the entry, open while one of its pages is current. */
  .nav-group {
    & .nav-chevron { margin-left: auto; --icon-size: 0.875rem; transition: rotate var(--dur) var(--ease); }
    &[open] .nav-chevron { rotate: 90deg; }
    &:has([aria-current="page"]) > .nav-item { color: var(--fg); font-weight: var(--weight-medium); }
  }
  .nav-sub {
    display: grid;
    gap: 1px;
    margin: 1px 0 var(--space-1) calc(var(--space-2) + 0.5rem);
    padding-left: var(--space-2);
    border-left: 1px solid var(--border);
  }
  .nav-sub-item {
    display: flex;
    align-items: center;
    height: 1.75rem;
    padding: 0 var(--space-2);
    font-size: var(--text-sm);
    color: var(--fg-muted);
    border-radius: var(--control-radius);
    transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
    &:hover { background: var(--surface-hover); color: var(--fg); }
    &[aria-current="page"] { color: var(--nav-active-fg); background: var(--nav-active-bg); font-weight: var(--weight-medium); }
  }
  .topbar {
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    height: 3.25rem;
    padding: 0 var(--space-5);
    background: color-mix(in oklab, var(--canvas) 85%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
    @media (width < 760px) { padding: 0 var(--space-4); }
  }
  .topbar-title { font-size: var(--text-md); font-weight: var(--weight-medium); }

  /* The main column's content: a stack of sections. */
  .page {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
    min-width: 0;
    padding: var(--space-6) var(--space-8) var(--space-12);
    @media (width < 760px) { padding: var(--space-4) var(--space-4) var(--space-10); }
  }
  /* A form page: one centered column. */
  .page-narrow {
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
    width: 100%;
    max-width: 42rem;
    margin-inline: auto;
  }
  .page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--space-3) var(--space-6);
    & .page-title { font-family: var(--font-display); font-size: var(--text-2xl); font-weight: var(--weight-heading); letter-spacing: var(--tracking-heading); line-height: var(--leading-tight); }
    & .page-sub { margin-top: var(--space-1); color: var(--fg-muted); font-size: var(--text-md); }
    & .page-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
    & .back-link { margin-bottom: var(--space-2); }
  }
  /* A detail page's two columns: the record, and a narrower aside beside
     it that drops below it on a narrow screen. */
  .main-aside {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 20rem;
    align-items: start;
    gap: var(--space-6);
    @media (width < 1080px) { grid-template-columns: minmax(0, 1fr); }
  }

  /* "Back to the list" above a page's title. */
  .back-link {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    font-size: var(--text-sm);
    color: var(--fg-muted);
    --icon-size: 0.875rem;
    &:hover { color: var(--fg); }
  }

  /* The sign-in page (Google mode): one card, centered. */
  .signin {
    display: grid;
    place-items: center;
    min-height: 100svh;
    padding: var(--space-6);
    & .signin-card { width: min(22rem, 100%); text-align: center; }
    & .brand-mark { align-self: center; width: 2.25rem; height: 2.25rem; font-size: var(--text-md); }
  }

  /* ------------------------------------------------- invoice document */

  .doc {
    --th-bg: transparent;
    padding: var(--space-10);
    background: var(--doc-bg, var(--surface));
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    box-shadow: var(--shadow-md);
    font-size: var(--text-md);
    @media (width < 760px) { padding: var(--space-5); }
    & .doc-head { display: flex; justify-content: space-between; gap: var(--space-6); flex-wrap: wrap; }
    & .doc-title { font-family: var(--font-display); font-size: var(--text-3xl); font-weight: var(--weight-heading); letter-spacing: var(--tracking-display); line-height: 1; }
    & .doc-totals { margin-left: auto; width: min(100%, 18rem); display: grid; grid-template-columns: 1fr auto; row-gap: var(--space-1-5); font-variant-numeric: tabular-nums; }
    & .doc-totals > :nth-child(even) { padding-left: var(--space-6); text-align: right; }
    & .doc-totals .grand { font-weight: var(--weight-strong); font-size: var(--text-lg); padding-top: var(--space-2); border-top: var(--total-rule); }
  }


  /* ================================================ portal components */

  /* ------------------------------------------------------- menu panels */

  /* A dropdown menu holding panels side by side with a rule between them
     (a date range's presets beside its custom-range form). On a narrow screen the
     panels stack and the menu is capped to the screen's width. */
  .menu-panels {
    display: flex;
    width: max-content;
    max-width: calc(100vw - var(--space-8));
    padding: 0;
    & > .menu-panel + .menu-panel { border-left: 1px solid var(--border-subtle); }
    @media (width < 760px) {
      flex-direction: column;
      width: min(18rem, calc(100vw - var(--space-8)));
      & > .menu-panel + .menu-panel { border-left: 0; border-top: 1px solid var(--border-subtle); }
    }
  }
  .menu-panel {
    display: flex;
    flex-direction: column;
    gap: var(--space-0-5);
    min-width: 10rem;
    padding: var(--space-2);
    & > .menu-label { padding-top: var(--space-1); }
    & .menu-item[aria-current] { font-weight: var(--weight-medium); background: var(--surface-muted); }
    & .menu-item[aria-current] > svg { margin-left: auto; }
  }
  /* A panel holding a small form rather than menu items. */
  .menu-panel-form {
    gap: var(--space-3);
    min-width: 13rem;
    padding: var(--space-2) var(--space-4) var(--space-4);
    & > .menu-label { padding-inline: 0; }
  }

  /* ----------------------------------------------------------- metrics */

  /* A row of headline figures, each an icon, a label and a value that
     wraps onto as many lines as the width needs. The value's color says
     its state (text-success, text-warning, text-danger, muted); a metric
     that is a link goes to the records behind it. */
  .metrics {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3) var(--space-6);
  }
  .metric {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    font-size: var(--text-md);
    white-space: nowrap;
    --icon-size: 1.125rem;
    & > svg { color: var(--fg-muted); }
    &:is(a) {
      border-radius: var(--radius-xs);
      transition: color var(--dur-fast) var(--ease);
      &:hover .metric-label { color: var(--fg); text-decoration: underline; text-underline-offset: 0.2em; }
    }
  }
  .metric-label { color: var(--fg-muted); }
  .metric-value { font-weight: var(--weight-strong); font-variant-numeric: tabular-nums; font-family: var(--font-figures); }

  /* ------------------------------------------------------------- panes */

  /* Sections of a card side by side, a rule between them, that stack
     (the rule then running across) when the card is too narrow. */
  .panes {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
    margin: calc(var(--space-4) * -1) calc(var(--card-pad) * -1);
    overflow: hidden;
  }
  .pane {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-width: 0;
    padding: var(--space-4) var(--card-pad);
    box-shadow: -1px 0 0 var(--border-subtle), 0 -1px 0 var(--border-subtle);
  }
  .pane-title { font-size: var(--text-xs); font-weight: var(--weight-medium); color: var(--fg-muted); }
  .pane-note {
    display: flex; align-items: center; gap: var(--space-2);
    font-size: var(--text-md);
    --icon-size: 1rem;
  }

  /* ---------------------------------------------------------- deadline */

  /* A dated item that needs doing: an icon, what it is and when, and its
     state as a badge. Overdue and due-soon items are tinted so they stand
     out from the ones that can wait. */
  .deadline {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    border: 1px solid transparent;
    border-radius: var(--control-radius);
    --icon-size: 1rem;
    & > svg { color: var(--fg-muted); }
  }
  .deadline-main { flex: 1; min-width: 0; }
  .deadline-title { font-size: var(--text-md); font-weight: var(--weight-medium); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .deadline-date { font-size: var(--text-xs); color: var(--fg-muted); font-variant-numeric: tabular-nums; }
  .deadline-paid > svg { color: var(--success); }
  .deadline-soon {
    background: var(--warning-soft);
    border-color: color-mix(in oklab, var(--warning) 30%, transparent);
    & > svg { color: var(--warning); }
  }
  .deadline-overdue {
    background: var(--danger-soft);
    border-color: color-mix(in oklab, var(--danger) 22%, transparent);
    & > svg { color: var(--danger); }
  }

  /* --------------------------------------------------------- cell clip */

  /* A table cell's text cut to one line at a fixed width, so a long
     message or address cannot widen its column. */
  .cell-clip {
    display: block;
    max-width: 20rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .cell-clip-sm { max-width: 13rem; }
  .cell-clip-xs { max-width: 8rem; }
  /* On the cell (not the clip) of the one column that takes the table's
     spare width: its text runs as far as the row allows, then clips. */
  .cell-fill {
    width: 100%;
    min-width: 12rem;
    & .cell-clip { width: 0; min-width: 100%; max-width: none; }
  }

  /* ---------------------------------------------------------- verbatim */

  /* Text someone wrote, shown as written: its line breaks kept and a long
     word or address broken rather than overflowing. */
  .verbatim { white-space: pre-line; overflow-wrap: anywhere; }

  /* ------------------------------------------------------ compact dl */

  /* A description list for a narrow column (a detail page's aside): a
     shorter label column, and values that break anywhere. */
  .dl-compact {
    grid-template-columns: minmax(5.5rem, max-content) minmax(0, 1fr);
    gap: var(--space-2) var(--space-3);
    align-items: baseline;
    & dd { overflow-wrap: anywhere; }
  }

  /* ----------------------------------------------------------- reorder */

  /* A list the user puts in order: each row a handle-less item with
     up/down buttons. Without JS each row shows its position as a number
     input instead (portal.js swaps them with the hidden attribute). */
  .reorder { display: grid; gap: var(--space-1); }
  .reorder-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: var(--control-h);
    padding: var(--space-1) var(--space-1) var(--space-1) var(--space-3);
    background: var(--surface-muted);
    border-radius: var(--control-radius);
    --icon-size: 1rem;
    & > svg { color: var(--fg-muted); }
  }
  .reorder-item > .reorder-pos { width: 4rem; flex: none; }
  .reorder-label { flex: 1; min-width: 0; font-size: var(--text-md); }
  .reorder-controls { display: flex; gap: var(--space-1); }

  /* ----------------------------------------------------------- icon tile */

  /* An icon on a small muted square that marks what a card is about (an
     import source, a kind of record) above its title. */
  .icon-tile {
    display: grid;
    place-items: center;
    flex-shrink: 0;
    width: 2.25rem;
    height: 2.25rem;
    color: var(--fg-muted);
    background: var(--surface-muted);
    border-radius: var(--control-radius);
    box-shadow: inset 0 0 0 1px var(--border-subtle);
    --icon-size: 1.25rem;
  }

  /* ---------------------------------------------------------- tree table */

  /* A table whose rows nest under group rows: invoice lines under their
     client and project. On the table beside .table: tr.tree-group heads a
     group, tr.tree-sub a group inside it; tree-1..3 on a first cell indent
     it one to three levels. A tr.tree-toggle shows and hides its
     tr.tree-detail rows (portal.js, data-toggle-rows); the chevron turns
     while they are hidden. */
  .tree {
    --tree-step: var(--space-5);
    --tree-start: calc(var(--cell-px) + var(--space-1));
    & tbody .tree-group, & tbody .tree-group:hover { background: var(--surface-muted); }
    & .tree-group td { font-weight: var(--weight-strong); }
    & tbody .tree-sub, & tbody .tree-sub:hover { background: color-mix(in oklab, var(--surface-muted) 45%, var(--surface)); }
    & .tree-sub td { font-weight: var(--weight-medium); color: var(--fg-muted); }
    & tbody .tree-detail { color: var(--fg-muted); background: color-mix(in oklab, var(--surface-muted) 60%, var(--surface)); }
    & .tree-toggle { cursor: pointer; }
    & tbody :is(th, td).tree-1 { padding-left: calc(var(--tree-start) + var(--tree-step)); }
    & tbody :is(th, td).tree-2 { padding-left: calc(var(--tree-start) + var(--tree-step) * 2); }
    & tbody :is(th, td).tree-3 { padding-left: calc(var(--tree-start) + var(--tree-step) * 3); }
  }
  .tree-chevron {
    --icon-size: 1rem;
    color: var(--fg-muted);
    transition: rotate var(--dur) var(--ease);
    tr[aria-expanded="false"] & { rotate: -90deg; }
  }

  /* ---------------------------------------------------------- file input */

  /* A native file picker sized like the other controls, its button drawn
     as a small secondary button inside the field. */
  .input[type="file"] {
    height: auto;
    min-height: var(--control-h);
    padding: var(--space-1) var(--space-1-5);
    color: var(--fg-muted);
    font-size: var(--text-sm);
    cursor: pointer;
    &::file-selector-button {
      height: var(--control-h-sm);
      margin-right: var(--space-3);
      padding: 0 var(--space-3);
      font: inherit;
      font-weight: var(--control-weight);
      color: var(--fg);
      background: var(--surface-muted);
      border: 1px solid var(--border);
      border-radius: calc(var(--control-radius) - 2px);
      cursor: pointer;
      transition: background-color var(--dur-fast) var(--ease);
    }
    &:hover::file-selector-button { background: var(--surface-hover); }
  }

  /* Checkbox filters drawn as pills: the label wraps a visually hidden
     checkbox, and a checked one is filled. The list's status filters. */
  .check-pills {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-1);
    & label {
      display: inline-flex;
      align-items: center;
      gap: var(--space-1-5);
      height: var(--control-h-sm);
      padding: 0 var(--space-3);
      font-size: var(--text-sm);
      font-weight: var(--weight-medium);
      color: var(--fg-muted);
      border: 1px solid var(--border);
      border-radius: var(--radius-full);
      cursor: pointer;
      transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
      &:hover { color: var(--fg); background: var(--surface-muted); }
      &:has(:checked) { color: var(--on-accent); background: var(--accent); border-color: var(--accent); }
      &:has(:focus-visible) { outline: var(--focus-width) solid var(--focus); outline-offset: var(--focus-offset); }
    }
    & .check-pills-sep { padding: 0 var(--space-1); color: var(--fg-subtle); }
  }

  /* A compact list of short lines in a table cell (a transaction's debit
     and credit legs), each with an optional small leading icon. */
  .mini-list {
    display: grid;
    gap: var(--space-0-5);
    font-size: var(--text-xs);
    --icon-size: 0.75rem;
    & li { display: flex; align-items: center; gap: var(--space-1); }
  }

  /* A cue at the foot of a link card ("View accounts ->"): small and muted,
     the arrow sized to the text; it darkens while its card is hovered. */
  .more-link {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    font-size: var(--text-xs);
    color: var(--fg-muted);
    --icon-size: 0.875rem;
    .card-interactive:hover & { color: var(--fg); }
  }

  /* Table row states: a row that no longer counts (a voided transaction, an
     inactive account), and one that needs a second look (an import's
     duplicate). */
  .table {
    & tbody .row-dimmed { opacity: 0.55; }
    & tbody .row-warning { background: var(--warning-soft); }
    & tbody .row-warning:hover { background: color-mix(in oklab, var(--warning-soft), var(--warning) 8%); }
  }

  /* A control whose value was filled in for the user and is known good: an
     import row's account picked by a matching rule. */
  .input-success {
    border-color: color-mix(in oklab, var(--success) 45%, var(--control-border));
    background-color: var(--success-soft);
  }

  /* Editable line items (journal lines): each row a grid of controls on a
     muted band, columns side / account / memo / amount; the rows stack
     their controls on a narrow screen. */
  .line-editor {
    display: grid;
    gap: var(--space-2);
  }
  .line-editor-row {
    display: grid;
    grid-template-columns: 7rem minmax(0, 1fr) minmax(0, 1fr) 8rem;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2);
    background: var(--surface-muted);
    border-radius: var(--control-radius);
    @media (width < 760px) {
      grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
      grid-auto-flow: row dense;
      & > :nth-child(2), & > :nth-child(3) { grid-column: 1 / -1; }
    }
  }

  /* ------------------------------------------------------------ figures */

  /* A short worked sum: lines of a label and its figure, an optional
     title above them, and a subtotal line under a rule (a tax breakdown,
     what was paid against what was owed). */
  .figures {
    display: grid;
    gap: var(--space-1);
    font-size: var(--text-md);
  }
  .figures-title { font-size: var(--text-md); font-weight: var(--weight-medium); margin-bottom: var(--space-1); }
  .figures-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-4);
    color: var(--fg-muted);
    & > :last-child {
      color: var(--fg);
      font-family: var(--font-figures);
      font-variant-numeric: tabular-nums;
      text-align: right;
      white-space: nowrap;
    }
  }
  .figures-total {
    padding-top: var(--space-1);
    border-top: 1px solid var(--border);
    color: var(--fg);
    font-weight: var(--weight-medium);
  }

  /* ------------------------------------------------------ coverage cell */

  /* One period in a coverage grid (a bank account's monthly statements):
     a small square, filled when the period has its record. A filled one
     is usually the link to that record. */
  .coverage-cell {
    display: block;
    width: 1rem;
    height: 1rem;
    margin-inline: auto;
    background: var(--surface-muted);
    border-radius: var(--radius-xs);
    box-shadow: inset 0 0 0 1px var(--border);
  }
  .coverage-cell-on {
    background: var(--success);
    box-shadow: none;
    transition: background-color var(--dur-fast) var(--ease);
    a:hover > & { background: color-mix(in oklab, var(--success), var(--fg) 20%); }
  }

  /* -------------------------------------------------------- plain list */

  /* A .list inside a padded container (a card body): the rows keep their
     rules but not the side padding a flush list needs. */
  .list { &.list-plain > li { padding-inline: 0; } }

  /* --------------------------------------------------------- table rows */

  /* The row for the period we are in (this quarter), tinted so the eye
     finds it. */
  .table tbody { & .row-current, & .row-current:hover { background: var(--surface-muted); } }

  /* A row's dropdown would be clipped by its table's scroller: while one
     is open, the scroller reaches far enough below the table to hold the
     menu (the negative margin keeps the page from moving). Only for a
     borderless scroller (in a card, or .bare): a framed one would show
     its frame stretching. */
  :is(.card .table-wrap, .table-wrap.bare):has(.dropdown[open]) {
    padding-bottom: 20rem;
    margin-bottom: -20rem;
  }

  /* ---------------------------------------------------------- total line */

  /* A report's bottom line: a label and its figure on one line, set large
     (net income, net profit, total assets against liabilities + equity).
     The figure's color may carry its sign (text-success, text-danger). */
  .total-line {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-1) var(--space-4);
    font-family: var(--font-display);
    font-size: var(--text-lg);
    font-weight: var(--weight-heading);
    letter-spacing: var(--tracking-heading);
    line-height: var(--leading-snug);
  }
  .total-line-value {
    font-family: var(--font-figures);
    font-size: var(--text-xl);
    font-weight: var(--weight-strong);
    font-variant-numeric: tabular-nums;
  }
}
