/**
 * ZEMM Generic Table Layouts
 *
 * Zentrale CSS-Regeln für konsistente Tabellen-Darstellung:
 * - Scroll-Container mit korrekter Hierarchie
 * - Header-Umbruch für lange Spaltennamen
 * - Responsive Spaltenbreiten
 * - Sticky Bottom-Bar für Pagination
 * - A11Y: Tastaturbedienung + Fokus
 *
 * Anwendung:
 * 1. Container mit [data-table-role="paginated"]
 * 2. .result-table-wrapper (scroll container)
 * 3. .table-scroll-x (table wrapper)
 * 4. table.table-fixed
 * 5. .table-pagination (sticky bottom)
 */

/* ==================== TABLE TOKENS ==================== */

:root {
  --table-font-size: 0.875rem;
  --table-font-size-dense: 0.8125rem;
  --table-header-font-size: 0.875rem;
  --table-cell-padding-y: 0.4rem;
  --table-cell-padding-x: 0.6rem;
  --table-header-padding-y: 0.5rem;
  --table-header-padding-x: 0.6rem;
  --table-border-color: rgba(27, 31, 35, 0.2);
  --table-border-color-strong: rgba(27, 31, 35, 0.3);
  --table-header-bg: #f6f8fa;
  --table-row-zebra: rgba(242, 245, 249, 0.6);
  --table-row-hover: rgba(90, 162, 255, 0.12);
  --table-focus-ring: #0969da;
  --table-action-width: 7.5rem;
  --table-checkbox-width: 2.75rem;
  --table-min-width: 960px;
  --table-radius: 8px;
  --table-wrapper-bg: rgba(255, 255, 255, 0.95);
  --table-wrapper-border: rgba(27, 31, 35, 0.25);
  --access-header-bg: #b9caec;
  --access-header-text: #1b2a44;
  --access-row-even: #e0e0e0;
  --access-row-odd: #f6f6f6;
  --access-border: rgba(27, 31, 35, 0.2);
  --access-grid-min-width: 960px;
}

/* ==================== SCROLL-HIERARCHIE FIX ==================== */

/**
 * Hauptcontainer: Flex-Column Layout für Scroll-Hierarchie
 * Controls oben → Scrollbarer Tabellenbereich → Sticky Pagination unten
 */
[data-table-role='paginated'] {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-height: 0; /* FLEX-BASIERT: flex:1 auf Parent gibt Höhe */
  flex: 1 1 auto; /* NEU: Nutzt verfügbare Höhe statt hardcoded calc() */
}

/**
 * Wrapper für scrollbare Tabelle
 * WICHTIG: Dieser Container bestimmt die Scroll-Höhe
 */
.result-table-wrapper,
.table-scroll-x,
[data-table-role='paginated'] > .table-scroll-x {
  flex: 1 1 auto; /* Nimmt alle verfügbare Höhe */
  min-height: 0; /* KRITISCH für flex-shrink */
  min-width: 0; /* Verhindert horizontales Overflow in Flex-Containern */
  overflow-x: auto; /* EINZIGER Scroll-Container */
  overflow-y: auto;
  border-radius: var(--table-radius);
  border: 1px solid var(--table-wrapper-border);
  background: var(--table-wrapper-bg);
  scroll-padding-bottom: 2rem;
  scrollbar-gutter: stable both-edges; /* FIX 4: Reserviert Platz für Scrollbar */
  scrollbar-width: thin; /* Schmaler Scrollbar */
}

/**
 * Generische Table-Wrapper (OUTtablelist, Legacy Lists)
 * Kein horizontaler Scroll im Hauptlayout: nur hier.
 */
.table-wrap,
.table-wrapper,
.virtual-table-wrapper {
  max-width: 100%;
  min-width: 0;
  overflow: auto;
  border-radius: var(--table-radius);
  border: 1px solid var(--table-wrapper-border);
  background: var(--table-wrapper-bg);
}

.table-wrap > table,
.table-wrapper > table,
.virtual-table-wrapper > table {
  min-width: 100%;
}

/**
 * Pagination: Sticky Bottom INNERHALB des Containers
 * Alternative: Außerhalb, dann als flex-shrink:0
 */
[data-table-role='paginated'] .table-pagination {
  flex: 0 0 auto; /* Nimmt nur benötigten Platz */
  margin-top: 0; /* Kein zusätzlicher Abstand */
  padding: 0.75rem 1rem;
  background: rgba(255, 255, 255, 0.98);
  border-top: 1px solid rgba(27, 31, 35, 0.15);
  border-radius: 0 0 8px 8px;
  /* Sticky: immer sichtbar am unteren Rand */
  position: sticky;
  bottom: 0;
  z-index: 3;
}

/* Verstecke Pagination wenn nur eine Seite */
[data-table-role='paginated'] .table-pagination.single-page {
  display: none;
}

/* ==================== GENERISCHE TABLE LAYOUTS ==================== */

/**
 * Fixed Table Layout: Gleichmäßige Spaltenbreiten, bessere Performance
 * WICHTIG: Funktioniert mit dynamischen Spalten (keine nth-child Hacks)
 */
table.table-fixed {
  width: 100%;
  min-width: var(--table-min-width); /* Mindestbreite für horizontales Scrollen */
  table-layout: fixed !important; /* FIX 5: FORCIERE Fixed-Layout */
  border-collapse: separate;
  border-spacing: 0;
  background: var(--content-bg, #ffffff);
}

/**
 * Auto Table Layout: Für dynamische Spaltenanzahl (z.B. Animals)
 * Spalten passen sich dem Inhalt an
 */
table.table-auto {
  width: max-content;
  min-width: 100%;
  table-layout: auto;
  border-collapse: separate;
  border-spacing: 0;
}

/* ==================== HEADER-UMBRUCH (Generisch!) ==================== */

/**
 * Tabellen-Header: Erlaubt Umbruch + Hyphens für lange Texte
 * KRITISCH: Entfernt white-space:nowrap für alle th
 */
table.table-fixed thead th,
table.table-auto thead th,
.table-scroll-x table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: var(--table-header-padding-y) var(--table-header-padding-x);
  background: var(--table-header-bg);
  color: #1f2328;
  font-weight: 600;
  font-size: var(--table-header-font-size);
  border-bottom: 2px solid var(--table-border-color-strong);
  /* FIX 3+5: FORCIERE Umbruch mit höchster Spezifität */
  white-space: normal !important; /* Überschreibt ui-fixes.css:1334 nowrap */
  word-wrap: break-word !important;
  overflow-wrap: anywhere !important;
  hyphens: auto;
  text-align: left;
  vertical-align: middle;
  line-height: 1.4;
}

/**
 * Sort-Header: Links dürfen umbrechen
 */
table.sortable th a.sortheader {
  display: inline-block;
  white-space: normal;
  word-wrap: break-word;
  line-height: 1.3;
}

/**
 * Sort-Arrow: Bleibt neben Text (kein Umbruch)
 */
table.sortable th .sortarrow {
  display: inline;
  white-space: nowrap;
  margin-left: 0.25rem;
}

/* ==================== ZELLENFORMATIERUNG ==================== */

/**
 * Standard-Zellen: Ellipsis für Overflow
 * AUSNAHME: Actions-Spalte (braucht Wrap für Buttons)
 */
table.table-fixed td,
table.table-auto td {
  padding: var(--table-cell-padding-y) var(--table-cell-padding-x);
  border-bottom: 1px solid var(--table-border-color);
  font-size: var(--table-font-size);
  vertical-align: middle;
  /* Default: Keine Ellipsis global, Wrap per Utility */
  text-overflow: clip;
}

/* Fixed Layout: erlaubt Umbruch (verhindert Ueberlauf in engen Spalten) */
table.table-fixed td {
  white-space: normal;
  overflow-wrap: anywhere;
  overflow: visible;
}

/* Auto Layout: kompakt, Spalten wachsen nach Inhalt */
table.table-auto td {
  white-space: nowrap;
  overflow: visible;
}

/**
 * Zellen, die umbrechen dürfen (Langtext/Kommentare)
 */
table.table-fixed td.cell-wrap,
table.table-auto td.cell-wrap,
table.table-fixed td.col-flex,
table.table-fixed td.col-description,
table.table-fixed td.col-comments,
table.table-fixed td.col-project,
table.table-fixed td.col-line,
table.table-fixed td.col-strain,
table.table-auto td.col-flex,
table.table-auto td.col-description,
table.table-auto td.col-comments,
table.table-auto td.col-project,
table.table-auto td.col-line,
table.table-auto td.col-strain {
  white-space: normal;
  overflow: visible;
  text-overflow: clip;
}

/**
 * Optionaler Truncate Helper
 */
td.cell-truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/**
 * Actions-Spalte: Buttons dürfen umbrechen
 */
table.table-fixed td.col-actions,
table.table-fixed td.col-action,
table.table-auto td.col-actions {
  white-space: normal;
  overflow: visible;
}

/**
 * Zebra-Streifen: Bessere Lesbarkeit
 */
table.table-fixed tbody tr:nth-child(even),
table.table-auto tbody tr:nth-child(even) {
  background: var(--table-row-zebra);
}

/**
 * Hover-Effekt: Zeile hervorheben
 */
table.table-fixed tbody tr:hover,
table.table-auto tbody tr:hover {
  background: var(--table-row-hover);
}

/* ==================== SPALTENKLASSEN (Generisch) ==================== */

/**
 * Generische Spalten-Klassen: Können in allen Tabellen verwendet werden
 * WICHTIG: Keine festen Pixelwerte, flexibel mit min-width
 */

/* Checkbox-Spalte */
.col-select,
table.table-fixed th:first-child,
table.table-fixed td:first-child,
table.table-auto th:first-child,
table.table-auto td:first-child {
  width: var(--table-checkbox-width);
  min-width: var(--table-checkbox-width);
  text-align: center;
}

/* Schmale Spalten: IDs, Counts, Dates */
.col-id,
.col-pop,
.col-count,
.col-num {
  width: 100px;
  min-width: 80px;
}

/* Mittelbreite: Namen, Typen */
.col-name,
.col-type,
.col-sex,
.col-cage {
  width: 140px;
  min-width: 100px;
}

/* Datum-Spalten */
.col-date,
.col-bday,
.col-death {
  width: 110px;
  min-width: 90px;
}

/* Breite Spalten: Projekte, Lizenzen, Linien */
.col-project,
.col-proj,
.col-license,
.col-line,
.col-strain,
.col-tag {
  width: 180px;
  min-width: 120px;
}

/* Extra-Breite: Flex-Spalten (Beschreibungen, etc.) */
.col-flex,
.col-description,
.col-comments {
  width: 280px;
  min-width: 160px;
}

/* Actions-Spalte: Breiter für Buttons */
.col-actions,
.col-action {
  width: var(--table-action-width);
  min-width: var(--table-action-width);
  text-align: center;
}

/* Zahlen-Spalten: Rechtsbündig */
.col-pop,
.col-count,
.col-num,
.text-right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Actions: Einheitliche Icon-Hit-Area */
.table-actions {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.table-actions a,
.table-actions button {
  min-width: 32px;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

/* ==================== ACCESS LISTS (Permissions) ==================== */

body.app-layout .access-grid {
  width: 100%;
  min-width: var(--access-grid-min-width);
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0.75rem 1rem;
}

body.app-layout .access-grid > tbody > tr > td {
  width: 33.333%;
  vertical-align: top;
  padding: 0;
  min-width: 0;
}

body.app-layout .access-list {
  display: block;
  width: 100%;
  height: clamp(220px, 36vh, 360px);
  min-height: 220px;
  overflow: auto;
  border: 1px solid var(--access-border);
  border-radius: 10px;
  background: var(--table-wrapper-bg);
  min-width: 0;
}

body.app-layout .access-grid > tbody > tr > th {
  text-align: left;
  font-weight: 600;
  color: var(--access-header-text);
  padding: 0 0.25rem;
  background: transparent;
}

body.app-layout .access-grid-wrap {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overflow-y: visible;
  padding-bottom: 0.25rem;
  scrollbar-gutter: stable both-edges;
}

body.app-layout .access-table {
  width: 100%;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  table-layout: auto;
}

body.app-layout .access-table th {
  background: var(--access-header-bg);
  color: var(--access-header-text);
  font-weight: 600;
  text-align: center;
  padding: 0.35rem 0.5rem;
}

body.app-layout .access-table th:first-child,
body.app-layout .access-table td:first-child {
  width: 28px;
  min-width: 28px;
  text-align: center;
}

body.app-layout .access-table th:last-child,
body.app-layout .access-table td:last-child {
  min-width: 180px;
}

body.app-layout .access-table td {
  padding: 0.25rem 0.5rem;
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
}

body.app-layout .access-table tr:nth-child(even) td {
  background: var(--access-row-even);
}

body.app-layout .access-table tr:nth-child(odd) td {
  background: var(--access-row-odd);
}

/* ==================== DENSE MODE (Reports) ==================== */

/**
 * Kompakte Tabellenansicht für Reports mit vielen Spalten
 * Anwendung: <table class="table-fixed table-dense">
 */
table.table-dense thead th {
  padding: 0.35rem 0.45rem;
  font-size: var(--table-font-size-dense);
}

table.table-dense td {
  padding: 0.3rem 0.45rem;
  font-size: var(--table-font-size-dense);
  line-height: 1.3;
}

/* Zahlen in Dense Mode: Tabellarische Zahlen */
table.table-dense .col-num,
table.table-dense .col-count,
table.table-dense .col-pop,
table.table-dense .text-right {
  font-variant-numeric: tabular-nums;
}

/* ==================== ACCESSIBILITY ==================== */

/**
 * Fokus-Indikatoren: Sichtbar für Tastaturbedienung
 */
table.table-fixed a:focus,
table.table-auto a:focus,
.table-pagination button:focus,
.table-pagination select:focus {
  outline: 2px solid var(--table-focus-ring);
  outline-offset: 2px;
  border-radius: 3px;
}

/**
 * Checkbox-Fokus
 */
table.table-fixed input[type='checkbox']:focus,
table.table-auto input[type='checkbox']:focus {
  outline: 2px solid var(--table-focus-ring);
  outline-offset: 1px;
}

table.table-fixed a:focus-visible,
table.table-auto a:focus-visible,
table.table-fixed button:focus-visible,
table.table-auto button:focus-visible,
table.table-fixed input:focus-visible,
table.table-auto input:focus-visible,
table.table-fixed select:focus-visible,
table.table-auto select:focus-visible {
  outline: 2px solid var(--table-focus-ring);
  outline-offset: 2px;
  border-radius: 4px;
}

/**
 * Skip-Link für lange Tabellen
 * Nutzer kann direkt zur Pagination springen
 */
.table-skip-link {
  position: absolute;
  left: -9999px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

.table-skip-link:focus {
  position: static;
  width: auto;
  height: auto;
  padding: 0.5rem 1rem;
  background: #0969da;
  color: white;
  font-weight: 600;
  text-decoration: none;
  z-index: 999;
}

/* ==================== PAGINATION KOMPONENTEN ==================== */

/**
 * Pagination Layout: Flexbox mit Gap
 */
.table-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.table-pagination__controls {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.table-pagination__controls label {
  font-weight: 600;
  font-size: 0.875rem;
}

.table-pagination__controls select {
  padding: 0.35rem 0.5rem;
  border: 1px solid rgba(27, 31, 35, 0.25);
  border-radius: 6px;
  font-size: 0.875rem;
  background: white;
}

.table-pagination__buttons {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
}

.table-pagination__buttons button {
  padding: 0.4rem 0.8rem;
  border: 1px solid rgba(27, 31, 35, 0.25);
  border-radius: 6px;
  background: white;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.table-pagination__buttons button:hover:not(:disabled) {
  background: #f6f8fa;
  border-color: rgba(27, 31, 35, 0.35);
}

.table-pagination__buttons button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.table-pagination__buttons span {
  font-size: 0.875rem;
  font-weight: 500;
  color: #57606a;
  min-width: 60px;
  text-align: center;
}

/* ==================== RESPONSIVE ADJUSTMENTS ==================== */

/**
 * Mobile: Kleinere Paddings, scrollbare Tabellen
 */
@media (max-width: 768px) {
  [data-table-role='paginated'] {
    max-height: calc(100vh - 220px);
  }

  table.table-fixed thead th,
  table.table-auto thead th {
    padding: 0.4rem 0.5rem;
    font-size: var(--table-font-size-dense);
  }

  table.table-fixed td,
  table.table-auto td {
    padding: 0.35rem 0.5rem;
    font-size: var(--table-font-size-dense);
  }

  .table-pagination {
    flex-direction: column;
    align-items: stretch;
  }

  .table-pagination__buttons {
    justify-content: center;
  }
}

/* ==================== HIGH-CONTRAST MODE ==================== */

/**
 * Barrierefreiheit: Höhere Kontraste für bessere Lesbarkeit
 */
@media (prefers-contrast: high) {
  table.table-fixed thead th,
  table.table-auto thead th {
    background: #e8eaed;
    border-bottom-width: 3px;
  }

  table.table-fixed td,
  table.table-auto td {
    border-bottom-width: 2px;
  }

  table.table-fixed tbody tr:hover,
  table.table-auto tbody tr:hover {
    background: rgba(90, 162, 255, 0.25);
  }
}

/* ==================== PRINT STYLES ==================== */

/**
 * Druck-Optimierung: Keine Schatten, kompakte Darstellung
 */
@media print {
  [data-table-role='paginated'] {
    max-height: none;
  }

  .result-table-wrapper,
  .table-scroll-x {
    overflow: visible;
    border: none;
  }

  .table-pagination {
    display: none;
  }

  table.table-fixed,
  table.table-auto {
    page-break-inside: auto;
  }

  table.table-fixed tr,
  table.table-auto tr {
    page-break-inside: avoid;
    page-break-after: auto;
  }
}
