@layer components {
  .icon { flex: none; }

  /* Buttons. Plain <button> and submit inputs already look like .button (base.css). */
  .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: var(--control-height);
    padding: 0 var(--space-3);
    background: var(--color-surface);
    color: var(--color-text);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    font-size: var(--text-base);
    font-weight: 500;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
  }

  .button:hover { background: var(--color-hover); color: var(--color-text); text-decoration: none; }

  .button--primary { background: var(--color-accent); border-color: var(--color-accent); color: #fff; }
  .button--primary:hover { background: var(--color-accent-hover); border-color: var(--color-accent-hover); color: #fff; }

  .button--ghost { background: transparent; border-color: transparent; box-shadow: none; color: var(--color-text-secondary); }
  .button--ghost:hover { background: var(--color-hover); color: var(--color-text); }

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

  .button--sm { min-height: 26px; padding: 0 var(--space-2); font-size: var(--text-sm); }
  .button--icon { width: 28px; min-height: 28px; padding: 0; }
  .button--block { width: 100%; justify-content: flex-start; }

  /* button_to wraps its button in a form; keep it inline. */
  form.inline { display: inline-flex; }

  /* Cards */
  .card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
  }

  .card__header {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border);
  }

  .card__header > .icon { margin-top: 2px; color: var(--color-text-tertiary); }
  .card__title { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
  .card__hint { font-size: var(--text-sm); color: var(--color-text-tertiary); }
  .card__body { padding: var(--space-4); display: flex; flex-direction: column; gap: var(--space-4); }
  .card__body--flush { padding: 0; }

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

  .subsection + .subsection { padding-top: var(--space-4); border-top: 1px solid var(--color-border); }
  .subsection { display: flex; flex-direction: column; gap: var(--space-3); }

  /* Forms */
  .form-stack { display: flex; flex-direction: column; gap: var(--space-4); max-width: 520px; }

  .field { display: flex; flex-direction: column; gap: 6px; }
  .field input:not([type="checkbox"]), .field select, .field textarea { width: 100%; }
  .field small { line-height: 1.4; }

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

  .inline-form { display: flex; align-items: center; flex-wrap: wrap; gap: var(--space-2); }
  .inline-form label { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; }
  .inline-form .grow { flex: 1 1 180px; min-width: 0; }
  .inline-form .w-code { width: 90px; }
  .inline-form .w-number { width: 110px; }

  .add-form {
    padding: var(--space-3);
    background: var(--color-bg);
    border: 1px dashed var(--color-border-strong);
    border-radius: var(--radius);
  }

  .input--code {
    font-family: var(--font-mono);
    font-size: var(--text-lg) !important;
    letter-spacing: 0.2em;
    text-align: center;
  }

  /* Lists: Linear-style compact rows. */
  .list { border: 1px solid var(--color-border); border-radius: var(--radius); background: var(--color-surface); }
  .card__body--flush > .list { border: 0; border-radius: 0 0 var(--radius-lg) var(--radius-lg); }

  .list-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 38px;
    padding: var(--space-1) var(--space-3);
    border-bottom: 1px solid var(--color-border);
  }

  .list-row:last-child { border-bottom: 0; }
  .list-row:hover { background: var(--color-hover); }

  .list-row__main { display: flex; align-items: center; gap: var(--space-2); flex: 1; min-width: 0; color: var(--color-text); }
  .list-row__main:hover { color: var(--color-text); text-decoration: none; }
  .list-row__main .icon { color: var(--color-text-tertiary); }
  .list-row__actions { display: flex; align-items: center; gap: var(--space-1); }

  a.list-row { color: var(--color-text); }
  a.list-row:hover { text-decoration: none; }

  .list-row__title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .list-row__code { font-family: var(--font-mono); font-size: var(--text-sm); color: var(--color-text-tertiary); white-space: nowrap; }
  .list-row__stack { display: flex; flex-direction: column; min-width: 0; }
  .list-row__sub { display: flex; align-items: center; gap: var(--space-1); min-width: 0; font-size: var(--text-sm); color: var(--color-text-tertiary); }
  .list-row__sub code { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .list-row__meta { margin-left: auto; color: var(--color-text-tertiary); font-size: var(--text-sm); white-space: nowrap; }

  .empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-6) var(--space-4);
    color: var(--color-text-tertiary);
    text-align: center;
  }

  .empty--inline { flex-direction: row; padding: var(--space-2) 0; font-size: var(--text-sm); }

  /* Badges */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 2px var(--space-2);
    border-radius: 999px;
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text-secondary);
    font-size: var(--text-sm);
    font-weight: 500;
    white-space: nowrap;
  }

  .badge .icon { width: 13px; height: 13px; }
  .badge--neutral .icon { color: var(--color-text-tertiary); }
  .badge--progress .icon { color: var(--color-warning); }
  .badge--accent .icon { color: var(--color-accent); }
  .badge--success .icon { color: var(--color-success); }
  .badge--danger { color: var(--color-danger); }
  .badge--danger .icon { color: var(--color-danger); }

  .tag {
    display: inline-flex;
    align-items: center;
    padding: 1px 6px;
    border-radius: var(--radius-sm);
    background: var(--color-hover);
    color: var(--color-text-secondary);
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    font-weight: 500;
  }

  .restricted {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    color: var(--color-text-tertiary);
    font-size: var(--text-sm);
    font-weight: 400;
  }

  .restricted .icon { width: 12px; height: 12px; }

  /* Key figures */
  .stat { display: flex; flex-direction: column; gap: 2px; }
  .card__body.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
  .stat__label { font-size: var(--text-sm); color: var(--color-text-tertiary); }
  .stat__value { font-size: var(--text-lg); font-weight: 600; font-variant-numeric: tabular-nums; }

  .num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

  /* Flash messages and form errors */
  .flashes { display: flex; flex-direction: column; gap: var(--space-2); }

  .flash {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    border: 1px solid;
    border-radius: var(--radius);
    font-weight: 500;
  }

  .flash > .icon { margin-top: 2px; }
  .flash__body { flex: 1; }
  .flash__body ul { display: flex; flex-direction: column; gap: 2px; }

  .flash--notice { background: var(--color-success-soft); border-color: #bfe5d0; color: #17603f; }
  .flash--alert { background: var(--color-danger-soft); border-color: #f3c7c7; color: #9b2c2c; }
  .flash--warning { background: var(--color-warning-soft); border-color: #f1d9ab; color: #7c4c06; }

  .flash__close {
    min-height: 22px;
    width: 22px;
    padding: 0;
    background: transparent;
    border: 0;
    box-shadow: none;
    color: inherit;
    opacity: 0.7;
  }

  .flash__close:hover { background: transparent; opacity: 1; }

  .secret {
    display: block;
    padding: var(--space-3);
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: var(--text-md);
    letter-spacing: 0.08em;
    text-align: center;
    user-select: all;
  }
}

@layer utilities {
  .visually-hidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }

  .table-scroll { overflow-x: auto; }
  .muted { color: var(--color-text-tertiary); }
  .step-reopen { display: flex; align-items: center; gap: var(--space-2); }
  .step-reopen > summary { list-style: none; cursor: pointer; }
  .step-reopen > summary::-webkit-details-marker { display: none; }
  .step-reopen[open] > summary { display: none; }
  .nowrap { white-space: nowrap; }
  .mono { font-family: var(--font-mono); }
  .row { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
}

@layer components {
  .list-row__main > form { flex: 1; min-width: 0; }

  /* Compact controls in row forms, so a row fits on one line at desktop width. */
  .inline-form input[type="text"]:not(.grow, .w-code), .inline-form input[type="email"] { width: 150px; }
  .inline-form input[type="date"] { width: 130px; }
  .inline-form select { max-width: 220px; }
  .inline-form .grow { flex: 1 1 140px; }
  .inline-form small { flex-basis: 100%; }
}

@layer components {
  /* Payment milestones: name, phase, percent, amount on one line; date, save, trigger hint below. */
  .milestone-form {
    display: grid;
    grid-template-columns: minmax(140px, 1fr) 180px 90px 140px;
    align-items: center;
  }

  .milestone-form > input:not([type="hidden"]), .milestone-form > select { width: 100%; max-width: none; }
  .milestone-form > input[type="submit"] { justify-self: start; width: auto; }
  .milestone-form > small { grid-column: span 2; }

  @media (max-width: 760px) {
    .milestone-form { grid-template-columns: 1fr 1fr; }
  }
}

@layer components {
  /* On narrow screens the restricted marker moves under the title instead of squeezing it. */
  .card__header { flex-wrap: wrap; }
  .card__title { flex: 1 1 200px; }
}
