/**
 * ====================================================================
 * FM-DATA-TABLE.CSS
 * ====================================================================
 * Handy-Tabelle (Baustein B2 aus doc/uiux/MOBILE-UX-KONZEPT.md), Variante „Kennzahl“.
 * Ein einziges <table>-Markup, alle Spalten bleiben im HTML:
 * - Handy/Tablet (< 992 px): Zeilenliste „Platz · Verein · eine Kennzahl“, Chips wählen die Kennzahl,
 *   eine Zeile klappt alle Werte auf. Kein seitliches Wischen, kein Scrollkasten.
 * - Desktop (ab 992 px): klassische Tabelle, Spaltenkopf klebt unter der festen Kopfzeile.
 * Verhalten: js/fm-data-table.js. Mobile-first, Erweiterung nur per min-width.
 * ====================================================================
 */

.fm-dt {
    --fm-dt-header-offset: 66px; /* Höhe der festen Kopfzeile (header.fixed-top) */
    --fm-dt-line: var(--fm-border-light);
    --fm-dt-gold-text: #84601f; /* Gold als Textfarbe, 5,7:1 auf Weiß */
    --fm-dt-row-hover: rgba(182, 139, 71, 0.08);
    --fm-dt-active-col: rgba(182, 139, 71, 0.07);
    --fm-dt-open-bg: #faf7f1;
    --fm-dt-touch: 2.75rem; /* 44 px */
    position: relative;
}

/* ====================================================================
 * KENNZAHL-CHIPS (nur mit JavaScript, nur unter 992 px)
 * ==================================================================== */
.fm-dt-toolbar {
    display: none;
}

.fm-dt.is-ready .fm-dt-toolbar {
    display: block;
    position: sticky;
    top: var(--fm-dt-header-offset);
    z-index: 1010;
    background-color: var(--fm-white);
    border-bottom: 1px solid var(--fm-dt-line);
    transition: top 0.25s ease;
}

/* Die Kopfzeile blendet sich auf dem Handy beim Herunterscrollen aus (app.js) */
body:has(header.fixed-top.navbar--hidden) .fm-dt.is-ready .fm-dt-toolbar {
    top: 0;
}

.fm-dt-chips {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem 0.5rem 0.25rem;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x proximity;
    scroll-padding-inline: 3rem; /* Chips nicht unter den Blätter-Pfeilen einrasten lassen */
    scrollbar-width: none;
}

.fm-dt-chips::-webkit-scrollbar {
    display: none;
}

/* Blätter-Pfeile am Rand der Chip-Leiste: zeigen, dass es weitergeht, und blättern beim Antippen.
   Nur sichtbar, solange in der Richtung noch Chips verdeckt sind (js/fm-data-table.js). */
.fm-dt-scroll {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    width: 3.5rem;
    padding: 0;
    border: 0;
    opacity: 0;
    pointer-events: none;
    touch-action: manipulation;
    transition: opacity 0.15s ease;
}

.fm-dt-scroll.is-visible {
    opacity: 1;
    pointer-events: auto;
}

.fm-dt-scroll--next {
    right: 0;
    justify-content: flex-end;
    padding-right: 0.25rem;
    background: linear-gradient(to right, rgba(255, 255, 255, 0), var(--fm-white) 55%);
}

.fm-dt-scroll--prev {
    left: 0;
    justify-content: flex-start;
    padding-left: 0.25rem;
    background: linear-gradient(to left, rgba(255, 255, 255, 0), var(--fm-white) 55%);
}

.fm-dt-scroll .bi {
    display: grid;
    place-items: center;
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    background-color: var(--fm-dark);
    color: var(--fm-gold);
    font-size: 0.875rem;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

.fm-dt-toolbar-label {
    flex: 0 0 auto;
    scroll-snap-align: start;
    font-size: 0.8125rem;
    color: var(--fm-text-secondary);
}

.fm-dt-chip {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    min-height: var(--fm-dt-touch);
    padding: 0 1rem;
    border: 1px solid var(--fm-dt-line);
    border-radius: 999px;
    background-color: var(--fm-white);
    color: var(--fm-text-primary);
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    scroll-snap-align: start;
    touch-action: manipulation;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.fm-dt-chip[aria-pressed="true"] {
    background-color: var(--fm-dark);
    border-color: var(--fm-dark);
    color: var(--fm-white);
}

.fm-dt-chip-dir {
    display: none;
    font-size: 0.75rem;
    color: var(--fm-gold);
}

.fm-dt-chip[aria-pressed="true"] .fm-dt-chip-dir {
    display: inline-block;
}

.fm-dt-chip:focus-visible,
.fm-dt-sort:focus-visible,
.fm-dt-toggle:focus-visible {
    outline: 2px solid var(--fm-gold);
    outline-offset: 2px;
}

/* ====================================================================
 * TABELLE ALS ZEILENLISTE (Basis = Handy)
 * ==================================================================== */
.fm-dt-table {
    display: block;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-variant-numeric: tabular-nums;
}

.fm-dt-table tbody {
    display: block;
    counter-reset: fm-dt-rank;
}

/* Spaltenkopf auf dem Handy nur für Screenreader, die Chips übernehmen das Sortieren */
.fm-dt-table thead {
    display: block;
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

.fm-dt-table tbody tr {
    display: grid;
    grid-template-columns: 1.75rem minmax(0, 1fr) auto;
    grid-template-areas: "rank club value";
    align-items: center;
    column-gap: 0.5rem;
    padding: 0.625rem 0.25rem;
    border-bottom: 1px solid var(--fm-dt-line);
    counter-increment: fm-dt-rank;
}

.fm-dt.is-ready .fm-dt-table tbody tr {
    grid-template-columns: 1.75rem minmax(0, 1fr) auto var(--fm-dt-touch);
    grid-template-areas: "rank club value more";
    padding-right: 0;
    cursor: pointer;
}

.fm-dt-table tbody > tr > * {
    display: none;
    padding: 0;
    font-weight: 400;
    text-align: left;
}

.fm-dt-table tbody .fm-dt-rank {
    display: block;
    grid-area: rank;
    text-align: center;
}

.fm-dt-table tbody .fm-dt-club {
    display: block;
    grid-area: club;
}

.fm-dt.is-ready .fm-dt-table tbody .fm-dt-more {
    display: block;
    grid-area: more;
}

/* Aufgeklappte Zeile: alle übrigen Kennzahlen als Liste unter dem Namen.
   Nur unter 992 px, damit eine offene Zeile nach dem Drehen des Tablets die Desktop-Tabelle nicht zerlegt. */
@media (max-width: 991.98px) {
    .fm-dt-table tbody tr.is-open {
        background-color: var(--fm-dt-open-bg);
    }

    .fm-dt-table tbody tr.is-open > .fm-dt-metric {
        display: flex;
        grid-column: 2 / 4;
        justify-content: space-between;
        align-items: baseline;
        gap: 1rem;
        padding: 0.4375rem 0;
        border-top: 1px dashed var(--fm-dt-line);
        font-size: 0.9375rem;
    }

    .fm-dt-table tbody tr.is-open > .fm-dt-metric::before {
        content: attr(data-label);
        color: var(--fm-text-secondary);
    }

    .fm-dt-table tbody tr.is-open > .fm-dt-metric .fm-dt-val {
        text-align: right;
    }

    .fm-dt-table tbody tr.is-open > .fm-dt-metric .fm-dt-sub {
        margin-left: 0.375rem;
    }

    .fm-dt-table tbody tr.is-open > .fm-dt-metric .fm-dt-bar {
        display: none;
    }
}

/* Gewählte Kennzahl rechts in der Zeile (steht nach der Aufklapp-Regel und gewinnt) */
.fm-dt-table tbody tr > .fm-dt-metric.is-active {
    display: block;
    grid-area: value;
    padding: 0;
    border-top: 0;
    font-size: inherit;
    text-align: right;
}

.fm-dt-table tbody tr > .fm-dt-metric.is-active::before {
    content: none;
}

.fm-dt-table tbody tr > .fm-dt-metric.is-active .fm-dt-num {
    display: block;
    font-size: 1rem;
    font-weight: 700;
    color: var(--fm-text-primary);
}

.fm-dt-table tbody tr > .fm-dt-metric.is-active .fm-dt-sub {
    display: block;
}

/* Zusätze, die in der zugeklappten Handy-Zeile zu viel Breite kosten (z. B. „Stand 22.08.2025“):
   erst beim Aufklappen und auf dem Desktop sichtbar */
.fm-dt-table tbody tr > .fm-dt-metric.is-active .fm-dt-sub--detail {
    display: none;
}

.fm-dt-table tbody tr.is-open > .fm-dt-metric.is-active .fm-dt-sub--detail {
    display: block;
}

/* ====================================================================
 * ZELLINHALTE
 * ==================================================================== */

/* Platz: Zähler, damit nach jedem Sortieren neu nummeriert wird */
.fm-dt-table tbody .fm-dt-rank::before {
    content: counter(fm-dt-rank);
    display: inline-grid;
    place-items: center;
    width: 1.75rem;
    height: 1.75rem;
    font-size: 0.9375rem;
    font-weight: 700;
    color: var(--fm-dt-gold-text);
}

.fm-dt-table tbody tr:nth-child(-n+3) .fm-dt-rank::before {
    border-radius: 50%;
    background-color: var(--fm-dark);
    color: var(--fm-gold);
}

.fm-dt-club-inner {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
}

.fm-dt-logo {
    flex-shrink: 0;
    width: 1.75rem;
    height: 1.75rem;
    object-fit: contain;
}

.fm-dt-names {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.fm-dt-team,
.fm-dt-stadium {
    display: block;
    min-height: 1.5rem;
    text-decoration: none;
    line-height: 1.5;
    -webkit-hyphens: auto;
    hyphens: auto;
    overflow-wrap: break-word;
}

.fm-dt-team {
    font-size: 1rem;
    font-weight: 700;
    color: var(--fm-text-primary);
}

.fm-dt-stadium {
    font-size: 0.875rem;
    color: var(--fm-text-secondary);
}

a.fm-dt-team:focus-visible,
a.fm-dt-stadium:focus-visible {
    outline: 2px solid var(--fm-gold);
    outline-offset: 1px;
    border-radius: 2px;
}

.fm-dt-num {
    white-space: nowrap;
}

.fm-dt abbr[title] {
    text-decoration: none;
    cursor: help;
}

.fm-dt-sub {
    font-size: 0.75rem;
    color: var(--fm-text-secondary);
    white-space: nowrap;
}

/* Balken für Kapazität und Stehplatzanteil (rein optisch, Zahl steht daneben) */
.fm-dt-bar {
    display: block;
    width: 4rem;
    height: 0.25rem;
    margin: 0.3125rem 0 0 auto;
    border: 0;
    border-radius: 2px;
    background-color: var(--fm-bg-light-alt);
    color: var(--fm-gold);
    -webkit-appearance: none;
    appearance: none;
    overflow: hidden;
}

.fm-dt-bar::-webkit-progress-bar {
    background-color: var(--fm-bg-light-alt);
    border-radius: 2px;
}

.fm-dt-bar::-webkit-progress-value {
    background-color: var(--fm-gold);
    border-radius: 2px;
}

.fm-dt-bar::-moz-progress-bar {
    background-color: var(--fm-gold);
    border-radius: 2px;
}

.fm-dt-toggle {
    display: grid;
    place-items: center;
    width: var(--fm-dt-touch);
    height: var(--fm-dt-touch);
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--fm-text-secondary);
    touch-action: manipulation;
}

.fm-dt-toggle .bi {
    transition: transform 0.2s ease;
}

.fm-dt-table tbody tr.is-open .fm-dt-toggle .bi {
    transform: rotate(180deg);
}

@media (hover: hover) {
    a.fm-dt-team:hover,
    a.fm-dt-stadium:hover {
        color: var(--fm-dt-gold-text);
        text-decoration: underline;
    }

    .fm-dt-chip:hover {
        border-color: var(--fm-gold);
    }

    .fm-dt-chip[aria-pressed="true"]:hover {
        border-color: var(--fm-dark);
    }
}

@media (prefers-reduced-motion: reduce) {
    .fm-dt.is-ready .fm-dt-toolbar,
    .fm-dt-scroll,
    .fm-dt-chip,
    .fm-dt-toggle .bi {
        transition: none;
    }
}

/* ====================================================================
 * TABLET QUER / KLEINES TABLET: etwas mehr Luft
 * ==================================================================== */
@media (min-width: 576px) {
    .fm-dt-chips {
        padding-left: 1rem;
    }

    .fm-dt-table tbody tr {
        padding-left: 1rem;
        column-gap: 0.875rem;
    }

    .fm-dt-logo {
        width: 2.25rem;
        height: 2.25rem;
    }

    .fm-dt-bar {
        width: 6rem;
    }
}

/* ====================================================================
 * DESKTOP: KLASSISCHE TABELLE
 * ==================================================================== */
@media (min-width: 992px) {
    .fm-dt.is-ready .fm-dt-toolbar {
        display: none;
    }

    .fm-dt-table {
        display: table;
        table-layout: auto;
    }

    .fm-dt-table thead {
        display: table-header-group;
        position: static;
        width: auto;
        height: auto;
        margin: 0;
        overflow: visible;
        clip-path: none;
        white-space: normal;
    }

    .fm-dt-table tbody {
        display: table-row-group;
    }

    .fm-dt-table tbody tr,
    .fm-dt.is-ready .fm-dt-table tbody tr {
        display: table-row;
        padding: 0;
        border: 0;
        cursor: auto;
    }

    .fm-dt-table thead th,
    .fm-dt-table tbody > tr > *,
    .fm-dt-table tbody tr > .fm-dt-metric.is-active,
    .fm-dt-table tbody .fm-dt-rank,
    .fm-dt-table tbody .fm-dt-club {
        display: table-cell;
        vertical-align: middle;
    }

    .fm-dt-table .fm-dt-more,
    .fm-dt.is-ready .fm-dt-table tbody .fm-dt-more {
        display: none;
    }

    /* Spaltenkopf: klebt unter der festen Kopfzeile, ganze Zelle ist der Sortierknopf */
    .fm-dt-table thead th {
        position: sticky;
        top: var(--fm-dt-header-offset);
        z-index: 2;
        padding: 0;
        background-color: var(--fm-dark);
        color: var(--fm-white);
        border-bottom: 3px solid var(--fm-gold);
        font-size: 0.8125rem;
        font-weight: 600;
        text-align: left;
        vertical-align: bottom;
    }

    .fm-dt-table thead th:first-child {
        border-top-left-radius: var(--fm-border-radius-lg);
        text-align: center;
        padding: 0.75rem 0.5rem;
    }

    .fm-dt-table thead th:nth-last-child(2) {
        border-top-right-radius: var(--fm-border-radius-lg);
    }

    .fm-dt-sort {
        display: flex;
        align-items: flex-end;
        gap: 0.25rem;
        width: 100%;
        min-height: 3.25rem;
        padding: 0.75rem 0.625rem;
        border: 0;
        background: transparent;
        color: inherit;
        font: inherit;
        line-height: 1.25;
        text-align: left;
        hyphens: manual;
    }

    .fm-dt-table thead .fm-dt-metric .fm-dt-sort {
        justify-content: flex-end;
        text-align: right;
    }

    .fm-dt-sort-icon {
        flex-shrink: 0;
        font-size: 0.75rem;
        opacity: 0.45;
    }

    .fm-dt-table thead th[aria-sort] {
        color: var(--fm-gold);
    }

    .fm-dt-table thead th[aria-sort] .fm-dt-sort-icon {
        opacity: 1;
    }

    .fm-dt-table tbody > tr > *,
    .fm-dt-table tbody tr > .fm-dt-metric.is-active {
        padding: 0.75rem 0.625rem;
        border-bottom: 1px solid var(--fm-dt-line);
        font-size: 0.9375rem;
    }

    .fm-dt-table tbody .fm-dt-rank {
        width: 3.5rem;
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    .fm-dt-table tbody .fm-dt-club {
        min-width: 15rem;
    }

    .fm-dt-table tbody > tr > .fm-dt-metric,
    .fm-dt-table tbody tr > .fm-dt-metric.is-active {
        text-align: right;
    }

    /* Werte: Zahl fett, Zusatz klein darunter; gleiche Optik unabhängig von der Sortierung */
    .fm-dt-table tbody .fm-dt-num,
    .fm-dt-table tbody tr > .fm-dt-metric.is-active .fm-dt-num {
        display: block;
        font-size: 0.9375rem;
        font-weight: 600;
        color: var(--fm-text-primary);
    }

    .fm-dt-table tbody .fm-dt-sub,
    .fm-dt-table tbody tr > .fm-dt-metric.is-active .fm-dt-sub,
    .fm-dt-table tbody tr > .fm-dt-metric.is-active .fm-dt-sub--detail {
        display: block;
    }

    .fm-dt-table tbody tr > .fm-dt-metric.is-active {
        background-color: var(--fm-dt-active-col);
    }

    .fm-dt-table tbody tr:last-child > * {
        border-bottom: 0;
    }

    .fm-dt-bar {
        width: 100%;
        max-width: 6rem;
    }
}

@media (min-width: 992px) and (hover: hover) {
    .fm-dt-table tbody tr:hover > * {
        background-color: var(--fm-dt-row-hover);
    }

    .fm-dt-sort {
        cursor: pointer;
    }

    .fm-dt-sort:hover .fm-dt-sort-icon {
        opacity: 1;
    }
}

@media (min-width: 1200px) {
    .fm-dt-table tbody > tr > *,
    .fm-dt-table tbody tr > .fm-dt-metric.is-active {
        padding: 0.875rem 0.875rem;
    }

    .fm-dt-sort {
        padding-left: 0.875rem;
        padding-right: 0.875rem;
    }

    .fm-dt-table tbody .fm-dt-club {
        min-width: 17rem;
    }
}

/* ====================================================================
 * SEITENRAHMEN, ZUSATZZEILE UND ERKLÄRUNGEN
 * ==================================================================== */

/* Abstand der Tabellen-Seite unter Brotkrumen; auf dem Handy laufen die Zeilen bis an den Rand */
.fm-dt-page {
    padding: 0.5rem 0 2rem;
}

.fm-dt {
    background-color: var(--fm-white);
}

/* Zusatzzeile unter dem Namen (z. B. „gegr. 1909 · AG“), nur in der Handy-Liste; ab 992 px stehen die Werte in Spalten */
.fm-dt-meta {
    display: block;
    font-size: 0.875rem;
    line-height: 1.5;
    color: var(--fm-text-secondary);
}

.fm-dt-legend {
    margin: 1.5rem 0.75rem 0;
    padding: 1rem 1.25rem;
    border-left: 3px solid var(--fm-gold);
    border-radius: var(--fm-border-radius-md);
    background-color: var(--fm-bg-light);
}

.fm-dt-legend-title {
    margin-bottom: 0.75rem;
    font-size: 1.0625rem;
    font-weight: 700;
}

.fm-dt-legend-list {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 0.375rem 1rem;
    margin: 0;
    font-size: 0.875rem;
}

.fm-dt-legend-list dt {
    font-weight: 700;
}

.fm-dt-legend-list dd {
    margin: 0;
    color: var(--fm-text-secondary);
}

.fm-dt-legend-note {
    margin: 0.75rem 0 0;
    font-size: 0.8125rem;
    color: var(--fm-text-secondary);
}

@media (min-width: 576px) {
    .fm-dt-page {
        padding-top: 1.5rem;
    }

    .fm-dt {
        overflow: clip;
        border: 1px solid var(--fm-border-light);
        border-radius: var(--fm-border-radius-lg);
        box-shadow: var(--fm-card-shadow-subtle);
    }

    .fm-dt-legend {
        margin-left: 0;
        margin-right: 0;
    }
}

@media (min-width: 992px) {
    .fm-dt-page {
        padding-top: 2rem;
    }

    /* Für Tabellen mit wenigen Spalten: nicht über die volle Breite ziehen */
    .fm-dt-page--narrow {
        max-width: 64rem;
        margin-inline: auto;
    }

    /* Ab hier klebt der Spaltenkopf an der Seite: kein Abschneiden durch den Rahmen */
    .fm-dt {
        overflow: visible;
        border: 0;
    }

    .fm-dt-meta {
        display: none;
    }
}

/* ====================================================================
 * DRUCK
 * ==================================================================== */
@media print {
    .fm-dt {
        border: 0;
        box-shadow: none;
    }

    .fm-dt-num,
    .fm-dt-team,
    .fm-dt-stadium {
        color: #000;
    }
    .fm-dt.is-ready .fm-dt-toolbar,
    .fm-dt.is-ready .fm-dt-table .fm-dt-more {
        display: none;
    }

    .fm-dt-table thead th {
        position: static;
        background: none;
        color: #000;
    }
}
