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

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

  .table {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    background-color: var(--surface);
    font-size: var(--text-md);
    border-radius: var(--radius-3);
  }

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

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

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

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

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

  /* Table head */
  .table__head {
    background-color: var(--neutral-100);
    border-bottom: 2px solid var(--neutral-200);
  }

  /* Table header cells */
  .table__header {
    padding: var(--space-4) var(--space-5);
    text-align: left;
    font-weight: var(--weight-semi);
    color: var(--fg);
    white-space: nowrap;
  }

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

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

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

  /* Table cells */
  .table__cell {
    padding: var(--space-4) var(--space-5);
    color: var(--fg);
    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(--neutral-400);
    font-style: italic;
    padding: var(--space-6);
  }

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

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

  /* Bordered table */
  .table--bordered {
    border-collapse: separate;
    border-spacing: 0;
    border: 1px solid var(--neutral-200);
    border-radius: var(--radius-3);
  }

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

  .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-3) - 1px);
  }

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

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

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

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

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

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

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

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

    .table__header,
    .table__cell {
      padding: var(--space-3) var(--space-4);
      font-size: var(--text-xs);
    }
  }

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

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

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