@layer components {
  /* ============================================================================
   * TABLE COMPONENT
   * Styled data tables with various display options
   * ========================================================================= */

  .table-container {
    width: 100%;
    overflow: auto hidden;
    border-radius: var(--radius-lg);
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on mobile */
  }

  .table {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    background-color: var(--color-bg-main);
    font-size: var(--font-size-md);
    border-radius: var(--radius-lg);
  }

  /* First row top corners */
  .table__head .table__row:first-child .table__header:first-child {
    border-top-left-radius: var(--radius-lg);
  }

  .table__head .table__row:first-child .table__header:last-child {
    border-top-right-radius: var(--radius-lg);
  }

  /* Last row bottom corners */
  .table__body .table__row:last-child .table__cell:first-child {
    border-bottom-left-radius: var(--radius-lg);
  }

  .table__body .table__row:last-child .table__cell:last-child {
    border-bottom-right-radius: var(--radius-lg);
  }

  /* Caption */
  .table__caption {
    padding: var(--space-md);
    text-align: left;
    font-weight: var(--font-weight-semibold);
    color: var(--color-dark);
    caption-side: top;
  }

  /* Table head */
  .table__head {
    background-color: var(--color-bg-highlight);
    border-bottom: 2px solid var(--color-gray-2);
  }

  /* Table header cells */
  .table__header {
    padding: var(--space-md) var(--space-lg);
    text-align: left;
    font-weight: var(--font-weight-semibold);
    color: var(--color-dark);
    white-space: nowrap;
  }

  /* Table body */
  .table__body {
    background-color: var(--color-bg-main);
  }

  /* Table rows */
  .table__row {
    border-bottom: 1px solid var(--color-gray-1);
    transition: background-color var(--transition-fast);
  }

  .table__body .table__row:last-child {
    border-bottom: none;
  }

  /* Table cells */
  .table__cell {
    padding: var(--space-md) var(--space-lg);
    color: var(--color-dark);
    vertical-align: top;
  }

  /* Force long text to wrap instead of expanding the table */
  .table-container--wrap-text .table {
    table-layout: fixed;
  }

  .table-container--wrap-text .table__cell,
  .table-container--wrap-text .table__header {
    overflow-wrap: anywhere;
  }

  /* Give the first column more space for long names */
  .table-container--wrap-text .table__header:first-child,
  .table-container--wrap-text .table__cell:first-child {
    width: 40%;
  }

  /* Empty state */
  .table__cell--empty {
    text-align: center;
    color: var(--color-gray-3);
    font-style: italic;
    padding: var(--space-xl);
  }

  /* ============================================================================
   * VARIANTS
   * ========================================================================= */

  /* Striped rows */
  .table--striped .table__body .table__row:nth-child(odd) {
    background-color: var(--color-bg-secondary);
  }

  /* Bordered table */
  .table--bordered {
    border-collapse: separate;
    border-spacing: 0;
    border: 1px solid var(--color-gray-2);
    border-radius: var(--radius-lg);
  }

  .table--bordered .table__header,
  .table--bordered .table__cell {
    border-right: 1px solid var(--color-gray-2);
    border-bottom: 1px solid var(--color-gray-2);
  }

  .table--bordered .table__header:last-child,
  .table--bordered .table__cell:last-child {
    border-right: none;
  }

  .table--bordered .table__body .table__row:last-child .table__cell {
    border-bottom: none;
  }

  /* Ensure rounded corners on bordered tables */
  .table--bordered
    .table__head
    .table__row:first-child
    .table__header:first-child {
    border-top-left-radius: calc(var(--radius-lg) - 1px);
  }

  .table--bordered
    .table__head
    .table__row:first-child
    .table__header:last-child {
    border-top-right-radius: calc(var(--radius-lg) - 1px);
  }

  .table--bordered
    .table__body
    .table__row:last-child
    .table__cell:first-child {
    border-bottom-left-radius: calc(var(--radius-lg) - 1px);
  }

  .table--bordered .table__body .table__row:last-child .table__cell:last-child {
    border-bottom-right-radius: calc(var(--radius-lg) - 1px);
  }

  /* Hoverable rows */
  .table--hoverable .table__body .table__row:hover {
    background-color: var(--color-brand-light);
    cursor: pointer;
  }

  /* Compact sizing */
  .table--compact .table__header,
  .table--compact .table__cell {
    padding: var(--space-sm) var(--space-md);
  }

  .table--compact {
    font-size: var(--font-size-sm);
  }

  /* ============================================================================
   * RESPONSIVE BEHAVIOR
   * ========================================================================= */

  @media (width <= 768px) {
    .table-container {
      border-radius: 0;
    }

    .table__header,
    .table__cell {
      padding: var(--space-sm) var(--space-md);
      font-size: var(--font-size-sm);
    }
  }

  /* ============================================================================
   * DARK MODE ADJUSTMENTS
   * ========================================================================= */

  @media (prefers-color-scheme: dark) {
    .table__head {
      background-color: var(--color-bg-highlight);
    }

    .table--striped .table__body .table__row:nth-child(odd) {
      background-color: var(--color-bg-secondary);
    }
  }
}
