@layer components {

  /* ─── Two-column layout ─────────────────── */

  .sidebar-layout {
    align-items: start;
    display: grid;
    gap: 1.5rem;
    grid-template-columns: 380px 1fr;
    padding-block-start: 2.5rem;
  }

  /* ─── Left panel: list ──────────────────── */

  .dir-list {
    background: var(--color-surface);
    border-radius: var(--radius-md);
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 1.25rem;
  }

  .dir-list__top {
    align-items: center;
    display: flex;
    gap: 0.5rem;
    margin-block-end: 0.75rem;
  }

  .dir-list__section-title {
    margin: 0.75rem 0 0.25rem;
    padding: 0 0.75rem;
  }

  .dir-list__letter {
    align-items: center;
    color: var(--color-ink-faint);
    display: flex;
    font-size: 0.75rem;
    font-weight: 600;
    gap: 0.625rem;
    list-style: none;
    margin: 0.75rem 0 0;
    padding: 0 0.25rem;
  }

  .dir-list__letter::after {
    background: var(--color-border);
    block-size: 1px;
    content: '';
    flex: 1;
  }

  .dir-list__empty {
    color: var(--color-ink-faint);
    font-size: 0.875rem;
    margin: 0;
    padding: 0.5rem 0.75rem;
  }

  .dir-list__items {
    display: flex;
    flex-direction: column;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  /* ─── List item ─────────────────────────── */

  .dir-list-item {
    border-block-end: 1px solid var(--color-border-subtle);
    list-style: none;
  }

  .dir-list-item:last-child {
    border-block-end: none;
  }

  .dir-list-item a {
    align-items: center;
    display: flex;
    gap: 0.75rem;
    /* Full-bleed: the active/hover background reaches the edge of the white
       card — cancels the 1.25rem padding of `.dir-list` and restores it as
       its own inline padding, so the text stays put (the card's 1.25rem +
       0.25rem = 1.5rem). */
    margin-inline: -1.25rem;
    padding: 0.625rem 1.5rem;
    text-decoration: none;
  }

  .dir-list-item a:hover {
    background: oklch(from var(--color-brand) l c h / 4%);
  }

  .dir-list-item--active a {
    background: var(--color-brand-tint);
  }

  /* ─── Avatar circle ─────────────────────── */

  .dir-list-item__avatar {
    align-items: center;
    background: var(--color-brand-tint);
    block-size: 38px;
    border-radius: 50%;
    color: var(--color-brand);
    display: flex;
    flex-shrink: 0;
    font-size: 0.875rem;
    font-weight: 600;
    inline-size: 38px;
    justify-content: center;
    overflow: hidden;
  }

  .dir-list-item__avatar-img {
    block-size: 100%;
    inline-size: 100%;
    object-fit: cover;
  }

  .dir-list-item--active .dir-list-item__avatar {
    background: var(--color-brand-fill);
    color: var(--color-ink-inverted);
  }

  .dir-list-item__text {
    flex: 1;
    min-inline-size: 0;
  }

  .dir-list-item__name {
    color: var(--color-ink);
    font-size: 1rem;
    font-weight: 500;
  }

  .dir-list-item--active .dir-list-item__name {
    color: var(--color-brand);
    font-weight: 600;
  }

  .dir-list-item--empty .dir-list-item__avatar {
    background: var(--color-surface-tint);
    color: var(--color-brand);
    font-style: normal;
    opacity: 0.6;
  }

  .dir-list-item--empty .dir-list-item__name {
    color: var(--color-brand);
  }

  .dir-list-item--empty a:hover {
    background: transparent;
  }

  .dir-list-item--empty a:hover .dir-list-item__name {
    text-decoration: underline;
  }

  .dir-list-item__ico {
    color: var(--color-ink-faint);
    font-size: 0.75rem;
    margin-block-start: 0.1rem;
  }

  /* ─── Right panel: profile detail ────────── */

  .dir-profile {
    background: var(--color-surface);
    border-radius: var(--radius-md);
    overflow: hidden;
  }

  .dir-profile__header {
    align-items: center;
    display: flex;
    gap: 1rem;
    padding: 1.5rem;
  }

  .dir-profile__avatar {
    align-items: center;
    background: var(--color-canvas);
    block-size: 64px;
    border: 1.5px solid oklch(from var(--color-brand) l c h / 20%);
    border-radius: 50%;
    display: flex;
    flex-shrink: 0;
    inline-size: 64px;
    justify-content: center;
    overflow: hidden;
  }

  /* Fallback while no logo is uploaded — the initial letter in the same
     spirit as the avatar in the list on the left (purple on a light circle). */
  .dir-profile__avatar-initial {
    color: var(--color-brand);
    font-size: 1.5rem;
    font-weight: 600;
    line-height: 1;
  }

  .dir-profile__avatar-photo {
    block-size: 100%;
    inline-size: 100%;
    object-fit: cover;
  }

  .dir-profile__info {
    flex: 1;
    min-inline-size: 0;
  }

  .dir-profile__name {
    color: var(--color-ink);
    font-size: 1.125rem;
    font-weight: 700;
    margin: 0 0 0.2rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .dir-profile__address {
    color: var(--color-ink-muted);
    font-size: 0.8125rem;
    line-height: 1.4;
    margin: 0;
  }

  /* button_to wraps the button in a <form> — display: contents keeps it a
     direct flex child of the header (for margin-left: auto and the mobile
     full width) */
  .dir-profile__invoice-form {
    display: contents;
  }

  .dir-profile__invoice-btn {
    flex-shrink: 0;
    margin-inline-start: auto;
  }

  /* button initiating the logo upload sits at the right of the name card;
     display: contents lets the <label> be a direct flex child of the header */
  .dir-profile__logo-form {
    display: contents;
  }

  /* Once a logo is uploaded ("Remove logo" exists), desktop shows only
     "Remove" and hides "Change" — so the header doesn't hold 3 buttons in
     a row. On mobile (≤768px) "Change" comes back below (both buttons
     stack). */
  .dir-profile__header:has(.dir-profile__logo-remove-btn) .dir-profile__logo-form {
    display: none;
  }

  .dir-profile__logo-btn {
    flex-shrink: 0;
  }

  /* wrapper is display: contents so it adds no gap when no logo is uploaded */
  #directories_profile_logo_removal,
  .dir-profile__logo-remove-form {
    display: contents;
  }

  /* ─── Profile form body ─────────────────── */

  .dir-profile__body {
    display: flex;
    flex-direction: column;
    font-size: 0.875rem;
    padding: 1.5rem 1.5rem 2rem;
  }

  .dir-profile__delete-form {
    margin-block-start: 1.5rem;
  }

  .dir-profile__delete-btn {
    background: none;
    border: none;
    color: var(--color-negative-strong);
    cursor: pointer;
    font-size: 0.8125rem;
    font-weight: 500;
    padding: 0.5rem 0;
  }

  .dir-profile__delete-btn:hover {
    text-decoration: underline;
  }

  /* ─── Documents tied to the contact ──────── */

  .dir-profile__documents {
    border-block-start: 1px solid var(--color-border-subtle);
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
    margin-block-start: 1.5rem;
    padding-block-start: 1.25rem;
  }

  .dir-profile__documents-title {
    align-items: center;
    color: var(--color-ink);
    display: flex;
    font-size: 0.9375rem;
    font-weight: 700;
    gap: 0.5rem;
    margin: 0;
  }

  .dir-profile__documents-count {
    background: var(--color-surface-tint);
    border-radius: var(--radius-pill);
    color: var(--color-brand);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.1rem 0.55rem;
  }

  .dir-profile__documents-empty {
    color: var(--color-ink-muted);
    font-size: 0.8125rem;
    margin: 0;
  }

  .dir-profile__documents-list {
    display: flex;
    flex-direction: column;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .dir-profile__document {
    align-items: baseline;
    border-block-end: 1px solid var(--color-border-subtle);
    display: grid;
    gap: 0.75rem;
    grid-template-columns: 1fr auto auto;
    padding: 0.45rem 0;
  }

  .dir-profile__document:last-child {
    border-block-end: none;
  }

  .dir-profile__document-link {
    color: var(--color-brand);
    font-weight: 600;
    text-decoration: none;
  }

  .dir-profile__document-link:hover {
    text-decoration: underline;
  }

  .dir-profile__document-date {
    color: var(--color-ink-muted);
    font-size: 0.8125rem;
  }

  .dir-profile__document-amount {
    font-variant-numeric: tabular-nums;
    font-weight: 600;
  }

  /* A section — the inner gap is smaller than the gap between sections */
  .dir-form-section {
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
  }

  /* The wrapper around the bank fields (it carries the `iban` controller) —
     `display: contents`, so its `.field-set`s fall straight into the
     `.dir-form-section` flex and get the same vertical spacing as the other
     sections (the wrapper would otherwise squash them). */
  .dir-form-fields {
    display: contents;
  }

  /* Override field-sets inside profile — higher specificity than field-sets.css */
  .dir-profile__body .field-set {
    column-gap: 0.625rem;
    row-gap: 0.25rem;
  }

  /* The inputs are `.inv-input` too, whose `--inv-input-height` is the
     invoice editor's; the directory sizes them by their text. Two classes to
     beat the editor's own rule. */
  .dir-profile__body .field-set input,
  .dir-profile__body .field-set select,
  .dir-profile__body .dir-contact-section input {
    field-sizing: content;
    min-block-size: 0;
    padding: 0.425rem 0.625rem;
  }

  /* A select sizes to the option it shows, so with nothing chosen it would
     collapse to a sliver, and browsers ignore its line-height — give it the
     height of the inputs beside it: their text line plus padding and borders. */
  .dir-profile__body .field-set select {
    field-sizing: fixed;
    min-block-size: calc(1.5em + 0.85rem + 3px);
  }

  /* ─── Relationship switcher ─────────────── */

  /* RELATIONSHIP and TYPE always stacked (at every breakpoint) — long labels
     like "Company, sole proprietor" then never wrap onto two lines. */
  .dir-vzah-row {
    display: grid;
    gap: 1rem;
    grid-template-columns: 1fr;
  }

  .dir-vzah-group {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
  }

  .dir-vzah-switcher {
    background: var(--color-canvas);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    display: flex;
    padding: 3px;
  }

  .dir-vzah-option {
    flex: 1;
    position: relative;
  }

  .dir-vzah-option__label {
    border-radius: var(--radius-sm);
    color: var(--color-ink-muted);
    cursor: pointer;
    display: block;
    font-size: 0.8125rem;
    font-weight: 500;
    letter-spacing: normal;
    padding: 0.35rem 0.75rem;
    text-align: center;
    text-transform: none;
    transition: background 0.15s, color 0.15s;
  }

  /* The radio is off screen; the keyboard's focus shows on its label. */
  .dir-vzah-input:focus-visible + .dir-vzah-option__label {
    outline: 2px solid var(--color-brand);
    outline-offset: -2px;
  }

  .dir-vzah-input:checked + .dir-vzah-option__label {
    background: var(--color-brand-fill);
    color: var(--color-ink-inverted);
  }

  /* ─── Form separators ───────────────────── */

  .dir-form-sep {
    /* Full-bleed to the card's edge — cancels the 1.5rem inline padding of
       `.dir-profile__body`. */
    border-block-start: 1.5px dashed var(--color-border-strong);
    margin-block: 1.5rem;
    margin-inline: -1.5rem;

    /* On desktop (two-column layout) more vertical room around the line. */
    @media (width >= 768px) {
      margin-block: 2.5rem;
    }
  }

  /* ─── Register entry ────────────────────── */

  /* The register entry is a line on the invoices the account issues, so it
     only belongs to its own company — hidden until the relationship switcher
     above says this profile is the supplier. Scoped under the body so it
     outranks the `.field-set` grid from field-sets.css, same layer. */
  .dir-profile__body .dir-register-field {
    display: none;
  }

  .dir-profile__body form:has(.dir-vzah-input[value="supplier"]:checked) .dir-register-field {
    display: grid;
  }

  /* ─── DIČ held by Peppol ────────────────── */

  .dir-held-note {
    color: var(--color-ink-muted);
    font-size: 0.75rem;
    line-height: 1.3;
    margin: 0;
    padding-block-start: 0.25rem;
  }

  /* ─── VAT settings ──────────────────────── */

  /* VAT settings head the chain every new invoice inherits from, so they only
     belong to the account's own company — hidden until the relationship
     switcher above says this profile is the supplier. */
  .dir-vat-section {
    display: none;
    flex-direction: column;
    gap: 0.375rem;
  }

  .dir-profile__body form:has(.dir-vzah-input[value="supplier"]:checked) .dir-vat-section {
    display: flex;
  }

  .dir-vat-section__title {
    margin: 0;
  }

  .dir-vat-section__hint {
    color: var(--color-ink-faint);
    font-size: 0.8125rem;
    margin: 0 0 0.5rem;
  }

  .dir-vat-modes {
    display: grid;
    gap: 0.625rem;
    grid-template-columns: 1fr;

    @media (width >= 768px) {
      grid-template-columns: repeat(3, 1fr);
    }
  }

  .dir-vat-mode {
    border: 1.5px solid var(--color-border);
    border-radius: var(--radius-md);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    padding: 0.75rem 0.875rem;
    position: relative;
    transition: border-color 0.15s, box-shadow 0.15s;
  }

  .dir-vat-mode:hover {
    border-color: var(--color-border-strong);
  }

  .dir-vat-mode:has(.dir-vat-mode__input:focus-visible) {
    outline: 2px solid var(--color-brand);
    outline-offset: 2px;
  }

  .dir-vat-mode:has(.dir-vat-mode__input:checked) {
    border-color: var(--color-brand);
    box-shadow: var(--shadow-focus);
  }

  .dir-vat-mode__title {
    color: var(--color-ink);
    font-size: 0.875rem;
    font-weight: 700;
  }

  .dir-vat-mode__desc {
    color: var(--color-ink-muted);
    font-size: 0.75rem;
    line-height: 1.35;
  }

  .dir-vat-rate {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    grid-column: 1 / -1;
    max-inline-size: 220px;
  }

  .dir-profile__body .dir-vat-rate input {
    padding: 0.425rem 0.625rem;
  }

  /* ─── Contact person section ────────────── */

  .dir-contact-section {
    background: var(--color-surface-tint);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 1rem 1.25rem;
  }

  .dir-contact-section__title {
    margin: 0;
  }

  /* ─── Empty state (index no profile) ────── */

  .dir-empty {
    align-items: center;
    color: var(--color-ink-faint);
    display: flex;
    font-size: 0.9375rem;
    justify-content: center;
    min-block-size: 300px;
  }

  /* ─── Bottom sheet close animation ─────── */

  /* Two classes on each: the opening animations below are set with the
     same selectors plus one class less, so these win the tie on specificity. */
  .dir-profile.dir-sheet--closing {
    animation: dir-sheet-out 0.35s cubic-bezier(0.4, 0, 1, 1) forwards;
  }

  .sidebar-layout__main:has(.dir-profile.dir-sheet--closing) {
    animation: dir-overlay-out 0.3s ease forwards;
  }

  /* ─── Back button (mobile only) ─────────── */

  .dir-profile__back-btn {
    display: none;
  }

  /* ─── Responsive ─────────────────────────── */

  @media (768px <= width < 1024px) {
    .sidebar-layout {
      gap: 1rem;
      grid-template-columns: 240px 1fr;
    }

    /* Collapse triple → double, keep double as-is */
    .dir-profile__body .field-set.triple,
    .dir-form-section .field-set.triple {
      grid-template-columns: 1fr 1fr;
    }

    /* Collapse double--left-heavy & double--right-heavy → equal halves */
    .dir-profile__body .field-set.double--left-heavy,
    .dir-form-section .field-set.double--left-heavy {
      grid-template-columns: 1fr 1fr;
    }
  }

  /* ─── Bottom sheet animations ──────────────── */

  @keyframes dir-sheet-in {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
  }

  @keyframes dir-sheet-out {
    from { transform: translateY(0); }
    to   { transform: translateY(100%); }
  }

  @keyframes dir-overlay-in {
    from { background: oklch(from var(--color-shadow) l c h / 0%); }
    to   { background: oklch(from var(--color-shadow) l c h / 35%); }
  }

  @keyframes dir-overlay-out {
    from { background: oklch(from var(--color-shadow) l c h / 35%); }
    to   { background: oklch(from var(--color-shadow) l c h / 0%); }
  }

  @media (width < 768px) {
    /* Single column layout */
    .sidebar-layout {
      display: block;
    }

    /* Hide empty state on mobile */
    .dir-empty {
      display: none;
    }

    /* Profile as bottom sheet overlay */
    .sidebar-layout__main:has(.dir-profile) {
      align-items: flex-end;
      animation: dir-overlay-in 0.3s ease both;
      display: flex;
      inset-block-end: 0;
      inset-block-start: 0;
      inset-inline-end: 0;
      inset-inline-start: 0;
      position: fixed;
      z-index: var(--z-sheet);
    }

    .dir-profile {
      animation: dir-sheet-in 0.4s cubic-bezier(0.16, 1, 0.3, 1) both;
      border-radius: var(--radius-xl) var(--radius-xl) 0 0;
      inline-size: 100%;
      max-block-size: 90dvh;
      overflow-y: auto;
    }

    /* The sheet scrolls on its own, so the body's room for the native apps'
       floating tab bar (layout.css) never reaches it: the end of the sheet,
       the delete button, would stay under the pill. */
    .dir-profile__body {
      padding-block-end: calc(2rem + var(--native-tab-bar-clearance, 0px));
    }

    /* Show back button on mobile */
    .dir-profile__back-btn {
      align-items: center;
      border-block-end: 1px solid var(--color-border-subtle);
      color: var(--color-brand);
      display: flex;
      font-size: 0.875rem;
      gap: 0.5rem;
      padding: 1rem 1.5rem;
      text-decoration: none;
    }

    /* Dynamic inline inputs on mobile — float side by side, wrap at row end */
    .dir-profile__body .field-set,
    .dir-form-section .field-set {
      align-items: flex-start;
      display: flex;
      flex-wrap: wrap;
      gap: 0.375rem 0.625rem;
    }

    /* Each label+input pair: label above, input below. The pair may shrink and
       is capped at the card: inputs size to their content, so a long
       placeholder (the register entry) or the longest option of a select (the
       legal form) would otherwise push the pair, and the card, out of the
       layout. */
    .dir-profile__body .field-set > div,
    .dir-form-section .field-set > div {
      display: flex;
      flex: 0 1 auto;
      flex-direction: column;
      gap: 0.25rem;
      max-inline-size: 100%;
      min-inline-size: 0;
    }

    /* Inputs: start as small box, grow with content, wrap at row end — never
       wider than the pair. */
    .dir-profile__body .field-set input,
    .dir-profile__body .field-set select,
    .dir-profile__body .dir-contact-section input,
    .dir-form-section .field-set input,
    .dir-form-section .field-set select {
      inline-size: auto;
      max-inline-size: 100%;
      min-inline-size: 68px;
    }

    .dir-profile__header {
      flex-wrap: wrap;
      gap: 0.75rem;
    }

    .dir-profile__invoice-btn {
      inline-size: 100%;
      justify-content: center;
      margin-inline-start: 0;
    }

    .dir-profile__logo-btn,
    .dir-profile__logo-remove-btn {
      inline-size: 100%;
      justify-content: center;
    }

    /* On mobile show both (Change + Remove) — they stack, the layout holds */
    .dir-profile__header:has(.dir-profile__logo-remove-btn) .dir-profile__logo-form {
      display: contents;
    }
  }
}
