@layer components {
  /* ======================
   * ATTACHMENT COMPONENTS
   * ======================
   * Thumbnail grids, attachment badges, and upload areas.
   */

  /* Attachment Grid */
  .attachment-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
    gap: var(--block-space);
  }

  .attachment-grid--compact {
    grid-template-columns: repeat(auto-fill, minmax(5rem, 1fr));
    gap: var(--block-space-sm);
  }

  /* Attachment Thumbnail */
  .attachment-thumbnail {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--radius-md);
    overflow: hidden;
    background-color: var(--color-canvas-alt);
    border: 1px solid var(--color-border);
  }

  .attachment-thumbnail__image-link {
    display: block;
    width: 100%;
    height: 100%;
  }

  .attachment-thumbnail__image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.15s ease;
  }

  .attachment-thumbnail:hover .attachment-thumbnail__image {
    transform: scale(1.05);
  }

  .attachment-thumbnail__overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: oklch(0% 0 0 / 0%);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease, background-color 0.15s ease;
  }

  .attachment-thumbnail:hover .attachment-thumbnail__overlay {
    background-color: oklch(0% 0 0 / 40%);
    opacity: 1;
    pointer-events: auto;
  }

  .attachment-thumbnail__action {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    border: none;
    background-color: white;
    color: var(--color-ink);
    border-radius: var(--radius-full);
    cursor: pointer;
    margin: 0.25rem;
    transition: background-color 0.15s ease;
  }

  .attachment-thumbnail__action:hover {
    background-color: var(--color-canvas-alt);
  }

  .attachment-thumbnail__action--danger:hover {
    background-color: var(--color-red);
    color: white;
  }

  /* Badge to show on animal row/card */
  .attachment-thumbnail__badge {
    position: absolute;
    top: var(--block-space-xs);
    left: var(--block-space-xs);
    padding: 0.125rem 0.375rem;
    background-color: oklch(0% 0 0 / 60%);
    color: white;
    font-size: var(--text-xs);
    font-weight: 500;
    border-radius: var(--radius-sm);
  }

  /* Attachment Badge - inline indicator for rows/cards */
  .attachment-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25ch;
    padding: 0.125rem 0.5rem;
    background-color: var(--color-canvas-alt);
    color: var(--color-ink-muted);
    font-size: var(--text-xs);
    border-radius: var(--radius-full);
    cursor: pointer;
    transition: background-color 0.15s ease;
  }

  .attachment-badge:hover {
    background-color: var(--color-border);
    color: var(--color-ink);
  }

  .attachment-badge__icon {
    font-size: 0.75em;
  }

  /* Attachment Upload Area */
  .attachment-upload {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--block-space-sm);
    padding: var(--block-space);
    border: 2px dashed var(--color-border);
    border-radius: var(--radius-md);
    background-color: var(--color-canvas-alt);
    cursor: pointer;
    transition: border-color 0.15s ease, background-color 0.15s ease;
    aspect-ratio: 1;
  }

  .attachment-upload:hover,
  .attachment-upload--active {
    border-color: var(--color-primary);
    background-color: oklch(55% 0.2 255 / 5%);
  }

  .attachment-upload__icon {
    font-size: var(--text-2xl);
    color: var(--color-ink-muted);
  }

  .attachment-upload__text {
    font-size: var(--text-sm);
    color: var(--color-ink-muted);
    text-align: center;
  }

  .attachment-upload__input {
    display: none;
  }

  /* Attachment Placeholder for empty state in grid */
  .attachment-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1;
    background-color: var(--color-canvas-alt);
    border: 2px dashed var(--color-border);
    border-radius: var(--radius-md);
    color: var(--color-ink-faint);
    font-size: var(--text-2xl);
  }

  /* Inline attachment list - for compact display */
  .attachment-list {
    display: flex;
    flex-wrap: wrap;
    gap: var(--block-space-sm);
  }

  .attachment-list__item {
    position: relative;
    width: 3rem;
    height: 3rem;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--color-border);
  }

  .attachment-list__item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .attachment-list__remove {
    position: absolute;
    top: -0.375rem;
    right: -0.375rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.25rem;
    height: 1.25rem;
    border: none;
    background-color: var(--color-red);
    color: white;
    border-radius: var(--radius-full);
    cursor: pointer;
    font-size: var(--text-xs);
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .attachment-list__item:hover .attachment-list__remove {
    opacity: 1;
  }

  /* Image count indicator */
  .image-count-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.375ch;
    color: var(--color-ink-muted);
    font-size: var(--text-sm);
  }

  .image-count-badge__icon {
    font-size: 0.9em;
  }

  /* Clickable variant - opens per-animal lightbox */
  .image-count-badge--clickable {
    cursor: pointer;
    border: none;
    background: none;
    padding: 0.125rem 0.375rem;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: var(--text-sm);
    transition: background-color 0.15s ease, color 0.15s ease;
  }

  .image-count-badge--clickable:hover {
    background-color: oklch(55% 0.2 255 / 10%);
    color: var(--color-primary);
  }

  /* ========================
   * ATTACHMENT HUB GRID
   * ========================
   * Card-based grid for the central images hub.
   */
  .attachment-hub-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
    gap: var(--block-space);
  }

  .attachment-hub-card {
    display: flex;
    flex-direction: column;
    background-color: var(--color-canvas);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    overflow: hidden;
  }

  .attachment-hub-card__image {
    position: relative;
    aspect-ratio: 1;
    overflow: hidden;
    background-color: var(--color-canvas-alt);
  }

  .attachment-hub-card__overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: oklch(0% 0 0 / 0%);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease, background-color 0.15s ease;
  }

  .attachment-hub-card:hover .attachment-hub-card__overlay {
    background-color: oklch(0% 0 0 / 40%);
    opacity: 1;
    pointer-events: auto;
  }

  .attachment-hub-card__img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.15s ease;
  }

  .attachment-hub-card:hover .attachment-hub-card__img {
    transform: scale(1.05);
  }

  .attachment-hub-card__info {
    padding: var(--block-space-sm);
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: var(--block-space-xs);
  }

  .attachment-hub-card__animal {
    font-weight: 600;
    font-size: var(--text-sm);
  }

  .attachment-hub-card__animal a {
    color: var(--color-ink);
    text-decoration: none;
  }

  .attachment-hub-card__animal a:hover {
    color: var(--color-link);
  }

  .attachment-hub-card__sheet {
    font-size: var(--text-xs);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .attachment-hub-card__sheet a {
    color: var(--color-ink-muted);
    text-decoration: none;
  }

  .attachment-hub-card__sheet a:hover {
    color: var(--color-link);
  }

  .attachment-hub-card__date {
    font-size: var(--text-xs);
    color: var(--color-ink-muted);
  }

  .attachment-hub-card__actions {
    display: flex;
    gap: var(--inline-space-sm);
    padding: var(--block-space-sm);
    border-block-start: 1px solid var(--color-border);
    background-color: var(--color-canvas-alt);
  }

  @media (max-width: 639px) {
    .attachment-hub-grid {
      grid-template-columns: repeat(2, 1fr);
    }
  }

  /* ========================
   * ATTACHMENT PREVIEW
   * ========================
   * Preview items during upload.
   */
  .attachment-preview {
    display: flex;
    flex-wrap: wrap;
    gap: var(--block-space-sm);
    margin-block-start: var(--block-space-sm);
  }

  .attachment-preview-item {
    position: relative;
    width: 3rem;
    height: 3rem;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--color-border);
  }

  .attachment-preview-item__image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.6;
  }

  .attachment-preview-item__spinner {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: oklch(0% 0 0 / 30%);
    color: white;
  }

  .attachment-preview-item--success .attachment-preview-item__image {
    opacity: 1;
  }

  .attachment-preview-item--success .attachment-preview-item__spinner {
    display: none;
  }

  .attachment-preview-item--error {
    border-color: var(--color-red);
  }

  .attachment-preview-item--error .attachment-preview-item__image {
    opacity: 0.3;
  }

  .attachment-preview-item--error .attachment-preview-item__spinner {
    background-color: oklch(55% 0.2 25 / 50%);
  }

  /* Upload error message */
  .attachment-upload-error {
    color: var(--color-red);
    font-size: var(--text-sm);
    margin-block-start: var(--block-space-xs);
  }

  /* ========================
   * PHOTO BADGE
   * ========================
   * Compact button showing photo count, opens dialog.
   */
  .attachment-badge-area {
    margin-block-start: var(--block-space-xs);
  }

  .photo-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.5rem;
    background-color: var(--color-canvas-alt);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-ink-muted);
    font-size: var(--text-xs);
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  }

  .photo-badge:hover {
    background-color: var(--color-canvas);
    border-color: var(--color-border-strong);
    color: var(--color-ink);
  }

  .photo-badge--has-photos {
    background-color: oklch(55% 0.2 255 / 10%);
    border-color: oklch(55% 0.2 255 / 30%);
    color: var(--color-primary);
  }

  .photo-badge--has-photos:hover {
    background-color: oklch(55% 0.2 255 / 15%);
    border-color: var(--color-primary);
  }

  .photo-badge__count {
    font-weight: 600;
  }

  /* ========================
   * PHOTO DIALOG
   * ========================
   * Dialog for viewing and managing photos.
   */
  .photo-dialog {
    max-width: min(90vw, 40rem);
  }

  .photo-dialog__upload {
    margin-block-end: var(--block-space);
  }

  .photo-dialog__upload-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5ch;
    padding: 0.5rem 1rem;
    background-color: var(--color-primary);
    color: white;
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.15s ease;
  }

  .photo-dialog__upload-btn:hover {
    background-color: oklch(50% 0.2 255);
  }

  .photo-dialog__upload-btn--secondary {
    background-color: var(--color-canvas-alt);
    color: var(--color-ink);
    border: 1px solid var(--color-border);
  }

  .photo-dialog__upload-btn--secondary:hover {
    background-color: var(--color-canvas);
    border-color: var(--color-border-strong);
  }

  .photo-badge-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375ch;
  }

  .photo-dialog__grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(6rem, 1fr));
    gap: var(--block-space-sm);
  }

  .photo-dialog__item {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--color-border);
  }

  .photo-dialog__image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.15s ease;
  }

  .photo-dialog__item:hover .photo-dialog__image {
    transform: scale(1.05);
  }

  .photo-dialog__remove {
    position: absolute;
    top: 0.25rem;
    right: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    border: none;
    background-color: oklch(0% 0 0 / 60%);
    color: white;
    border-radius: var(--radius-sm);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s ease, background-color 0.15s ease;
    font-size: var(--text-xs);
  }

  .photo-dialog__item:hover .photo-dialog__remove {
    opacity: 1;
  }

  .photo-dialog__remove:hover {
    background-color: var(--color-red);
  }

  .photo-dialog__image-link {
    display: block;
    width: 100%;
    height: 100%;
  }

  .photo-dialog__item-actions {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: center;
    gap: var(--inline-space-sm);
    padding: var(--block-space-xs);
    background: linear-gradient(to top, oklch(0% 0 0 / 70%), transparent);
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .photo-dialog__item:hover .photo-dialog__item-actions {
    opacity: 1;
  }

  .photo-dialog__action {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    border: none;
    background-color: white;
    color: var(--color-ink);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: var(--text-sm);
    text-decoration: none;
    transition: background-color 0.15s ease;
  }

  .photo-dialog__action:hover {
    background-color: var(--color-canvas-alt);
  }

  .photo-dialog__action--danger:hover {
    background-color: var(--color-red);
    color: white;
  }

  .photo-dialog__empty {
    grid-column: 1 / -1;
    text-align: center;
    color: var(--color-ink-muted);
    padding: var(--block-space-lg);
  }

  /* Mobile upload buttons side by side */
  .photo-dialog__upload--mobile {
    display: flex;
    gap: var(--inline-space);
  }

  .photo-dialog__upload--mobile .photo-dialog__upload-btn {
    flex: 1;
    justify-content: center;
  }

  /* Mobile: always show action buttons */
  .photo-dialog__item-actions--mobile {
    opacity: 1;
  }
}
