/* ==========================================================================
   INTEL-TABLE — reusable searchable / filterable / sortable table component
   Paired with static/js/intel-table.js (window.IntelTable). Consumed by the
   Pal / Passive / Active / Item libraries and the breeding combo table in
   later phases. Token-driven off t7-theme.css (do NOT redeclare the palette).
   Every container is overflow-safe; wide tables scroll inside their own
   horizontal scroller so the page body never scrolls sideways.
   ========================================================================== */

.intel-table-wrap {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    box-sizing: border-box;
    min-width: 0;
}

/* --- Controls row: search box + facet filters --------------------------- */
.intel-table-controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    margin-bottom: 16px;
    background: rgba(15, 5, 5, 0.8);
    border: 1px solid var(--glass-border);
    border-radius: 10px;
    box-sizing: border-box;
    min-width: 0;
}

.intel-table-search {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1 1 240px;
    min-width: 0;
    background: rgba(10, 15, 22, 0.85);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 8px;
    padding: 8px 14px;
}
.intel-table-search i { color: #8a7f6e; flex-shrink: 0; }
.intel-table-search input {
    flex: 1 1 auto;
    min-width: 0;
    width: 100%;
    background: transparent;
    border: none;
    outline: none;
    color: #fff;
    font-family: inherit;
    font-size: 0.95em;
}
.intel-table-search input::placeholder { color: #8a7f6e; }

.intel-table-facets {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    min-width: 0;
}
.intel-facet {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}
.intel-facet label {
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: rgba(240, 224, 196, 0.6);
    white-space: nowrap;
}
.intel-facet select {
    background: #140505;
    color: #f1e6d2;
    border: 1px solid var(--glass-border);
    border-radius: 6px;
    padding: 6px 10px;
    font-family: inherit;
    font-size: 0.85em;
    outline: none;
    max-width: 180px;
    min-width: 0;
}
.intel-facet select:focus { border-color: var(--fire-orange); }

.intel-table-count {
    margin-left: auto;
    font-size: 0.82rem;
    color: rgba(240, 224, 196, 0.65);
    white-space: nowrap;
    flex-shrink: 0;
}

/* --- Scroller: the ONLY element allowed to scroll horizontally ---------- */
.intel-table-scroll {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    background: rgba(10, 10, 15, 0.72);
    -webkit-overflow-scrolling: touch;
    min-width: 0;
}
.intel-table-scroll::-webkit-scrollbar { height: 8px; }
.intel-table-scroll::-webkit-scrollbar-track { background: rgba(0, 0, 0, 0.2); border-radius: 4px; }
.intel-table-scroll::-webkit-scrollbar-thumb { background: var(--fire-orange); border-radius: 4px; }

/* --- The table ---------------------------------------------------------- */
table.intel-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9em;
    color: #eadfce;
}
table.intel-table thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: rgba(25, 8, 4, 0.98);
    color: var(--fire-yellow);
    text-align: left;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-size: 0.78rem;
    padding: 12px 14px;
    border-bottom: 1px solid var(--glass-border);
    white-space: nowrap;
}
table.intel-table thead th.is-sortable { cursor: pointer; user-select: none; }
table.intel-table thead th.is-sortable:hover { color: #fff; }
table.intel-table thead th .intel-sort-caret {
    margin-left: 6px;
    font-size: 0.72em;
    opacity: 0.4;
}
table.intel-table thead th[aria-sort="ascending"] .intel-sort-caret,
table.intel-table thead th[aria-sort="descending"] .intel-sort-caret {
    opacity: 1;
    color: var(--fire-orange);
}
table.intel-table tbody td {
    padding: 10px 14px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    vertical-align: middle;
    max-width: 320px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
table.intel-table tbody tr {
    transition: background 0.15s ease;
}
table.intel-table tbody tr:hover { background: rgba(255, 69, 0, 0.08); }
table.intel-table tbody tr:nth-child(even) { background: rgba(255, 255, 255, 0.02); }
table.intel-table tbody tr:nth-child(even):hover { background: rgba(255, 69, 0, 0.08); }

/* Thumbnail cell (Items / Pals) */
.intel-table img.intel-cell-thumb {
    width: 38px;
    height: 38px;
    object-fit: contain;
    display: block;
    background: rgba(0, 0, 0, 0.35);
    border-radius: 8px;
    padding: 3px;
    box-sizing: border-box;
}

/* Small pill/badge used inside cells (tier, category, rarity, element...) */
.intel-cell-badge {
    display: inline-block;
    font-size: 0.72rem;
    font-weight: 700;
    padding: 3px 10px;
    border-radius: 999px;
    border: 1px solid var(--glass-border);
    background: rgba(255, 220, 150, 0.06);
    color: #f1e6d2;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: middle;
}

/* --- Empty + loading states -------------------------------------------- */
.intel-table-empty,
.intel-table-loading {
    padding: 42px 20px;
    text-align: center;
    color: rgba(240, 224, 196, 0.7);
    font-size: 0.95rem;
}
.intel-table-empty i,
.intel-table-loading i {
    display: block;
    font-size: 2.2rem;
    margin-bottom: 12px;
    color: var(--fire-orange);
    opacity: 0.7;
}
.intel-table-loading i { animation: intel-spin 1s linear infinite; }
@keyframes intel-spin { to { transform: rotate(360deg); } }

/* --- Pagination --------------------------------------------------------- */
.intel-table-pager {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-top: 16px;
    min-width: 0;
}
.intel-table-pager button {
    background: rgba(20, 4, 2, 0.85);
    border: 1px solid var(--glass-border);
    color: #f1e6d2;
    padding: 8px 14px;
    border-radius: 8px;
    font-family: inherit;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s ease;
    min-width: 0;
}
.intel-table-pager button:hover:not(:disabled) {
    border-color: var(--fire-orange);
    color: #fff;
    background: rgba(255, 69, 0, 0.12);
}
.intel-table-pager button:disabled { opacity: 0.4; cursor: not-allowed; }
.intel-table-pager .intel-pager-status {
    font-size: 0.85rem;
    color: rgba(240, 224, 196, 0.7);
    white-space: nowrap;
}

/* --- Table-mode mobile sort control ------------------------------------- */
/* Hidden on desktop (headers are clickable there). Revealed only when the
   table collapses into stacked cards, where the <thead> is visually hidden. */
.intel-table-sort-wrap { display: none; }
.intel-table-sort { min-width: 130px; }
.intel-table-sort-dir {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 32px;
    flex: 0 0 auto;
    background: #140505;
    color: #f1e6d2;
    border: 1px solid var(--glass-border);
    border-radius: 6px;
    cursor: pointer;
    transition: border-color 0.18s ease, color 0.18s ease, background 0.18s ease;
}
.intel-table-sort-dir:hover:not(:disabled) {
    border-color: var(--fire-orange);
    color: #fff;
    background: rgba(255, 69, 0, 0.12);
}
.intel-table-sort-dir:disabled { opacity: 0.4; cursor: not-allowed; }
.intel-table-sort-dir:focus-visible { outline: 2px solid var(--fire-yellow); outline-offset: 1px; }

/* --- Responsive --------------------------------------------------------- */
@media (max-width: 720px) {
    .intel-table-controls { padding: 10px 12px; }
    .intel-table-count { margin-left: 0; width: 100%; }
    table.intel-table { font-size: 0.82em; }

    /* Reveal the mobile sort control (the clickable header row is hidden). */
    .intel-table-sort-wrap { display: inline-flex; }

    /* ------------------------------------------------------------------
       STACKED-CARD VIEW. Below 720px each <tr> collapses into a bordered
       vertical card and every <td> becomes a "Column: value" row, with the
       column name pulled from data-label (emitted + escaped by the JS) via
       CSS attr(). Result: EVERY column stays visible with ZERO horizontal
       scroll and zero hidden data. Desktop keeps the full wide table.
       ------------------------------------------------------------------ */

    /* The scroller no longer needs to scroll — cards fit the viewport. */
    .intel-table-scroll {
        overflow-x: visible;
        border: none;
        background: transparent;
        border-radius: 0;
    }

    table.intel-table,
    table.intel-table tbody { display: block; width: 100%; }

    /* Keep the header in the DOM (a11y/desktop) but pull it out of view. */
    table.intel-table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        margin: -1px;
        padding: 0;
        border: 0;
        overflow: hidden;
        clip: rect(0 0 0 0);
        clip-path: inset(50%);
        white-space: nowrap;
    }

    table.intel-table tbody tr,
    table.intel-table tbody tr:nth-child(even) {
        display: block;
        margin: 0 0 14px;
        padding: 4px 0;
        border: 1px solid var(--glass-border);
        border-radius: 12px;
        background: rgba(10, 10, 15, 0.72);
        overflow: hidden;
    }
    table.intel-table tbody tr:hover,
    table.intel-table tbody tr:nth-child(even):hover { background: rgba(10, 10, 15, 0.72); }

    table.intel-table tbody td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 14px;
        width: 100%;
        max-width: none;      /* undo the desktop ellipsis clamp — show everything */
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        text-align: right;
        padding: 9px 16px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    }
    table.intel-table tbody td:last-child { border-bottom: none; }

    /* The row's primary name link is the main way into a detail page from a
       table on a phone, but as bare inline text it was only a 15px-tall tap
       target (measured on /intelligence/skills/active and /stats) — under the
       24px WCAG 2.5.8 minimum, in a cell that was already 34px tall. Give the
       link the cell's full height instead of adding page height: the floor here
       costs ~6-10px per row, not a new row. Direct children only, so cells that
       hold a LIST of links (e.g. the drop cells) keep their compact stacking
       rather than each entry claiming 44px. */
    table.intel-table tbody td > .intel-name-link {
        display: inline-flex;
        align-items: center;
        min-height: 44px;
    }

    /* Column-label rail from data-label. attr() renders it as PLAIN TEXT, so
       the value can never inject markup (XSS-safe by construction). */
    table.intel-table tbody td::before {
        content: attr(data-label);
        flex: 0 0 42%;
        text-align: left;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        font-size: 0.68rem;
        line-height: 1.3;
        color: var(--fire-yellow);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    /* Unlabeled cells (e.g. the thumbnail column) drop the rail and center. */
    table.intel-table tbody td[data-label=""]::before,
    table.intel-table tbody td:not([data-label])::before { content: none; }
    table.intel-table tbody td[data-label=""] { justify-content: center; }

    /* Empty / loading state cells span the card without the label rail. */
    table.intel-table tbody td .intel-table-empty,
    table.intel-table tbody td .intel-table-loading { width: 100%; }
    table.intel-table tbody tr:has(.intel-table-empty),
    table.intel-table tbody tr:has(.intel-table-loading) {
        border: none;
        background: transparent;
    }
}

/* ==========================================================================
   OPT-IN EXTENSIONS — card grid (1A), chip facets (1B), detail drawer (1C).
   All token-driven off t7-theme.css, overflow-safe, mobile-safe. These rules
   only style markup that the new JS emits; table-only pages never see them.
   ========================================================================== */

/* --- 1A: Card grid ------------------------------------------------------ */
.intel-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 14px;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    min-width: 0;
}
/* Empty / loading / error message spans the full grid row. */
.intel-grid > .intel-table-empty,
.intel-grid > .intel-table-loading { grid-column: 1 / -1; }

.intel-card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 14px;
    box-sizing: border-box;
    min-width: 0;
    overflow: hidden;
    background:
        linear-gradient(180deg, rgba(20, 4, 2, 0.85), rgba(14, 2, 2, 0.7)),
        radial-gradient(circle at top left, rgba(255, 69, 0, 0.10), transparent 46%);
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    color: #eadfce;
    transition: transform 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease;
}
.intel-card.intel-card-clickable { cursor: pointer; }
.intel-card.intel-card-clickable:hover {
    transform: translateY(-3px);
    border-color: var(--fire-orange);
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45), 0 0 18px rgba(255, 140, 0, 0.22);
}
.intel-card.intel-card-clickable:focus-visible {
    outline: 2px solid var(--fire-yellow);
    outline-offset: 2px;
    border-color: var(--fire-orange);
}

/* Grid-mode sort <select> (reuses .intel-facet select base above). */
.intel-grid-sort { min-width: 150px; }

/* --- 1B: Chip facets ---------------------------------------------------- */
.intel-facet-chips { align-items: center; flex-wrap: wrap; }
.intel-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    min-width: 0;
}
.intel-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    border-radius: 999px;
    border: 1px solid var(--glass-border);
    background: rgba(255, 220, 150, 0.05);
    color: #f1e6d2;
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1.1;
    cursor: pointer;
    transition: border-color 0.18s ease, background 0.18s ease, color 0.18s ease, box-shadow 0.18s ease;
    min-width: 0;
    max-width: 100%;
}
.intel-chip:hover {
    border-color: var(--fire-orange);
    background: rgba(255, 120, 30, 0.12);
    color: #fff;
}
.intel-chip:focus-visible {
    outline: 2px solid var(--fire-yellow);
    outline-offset: 1px;
}
.intel-chip[aria-pressed="true"] {
    border-color: var(--fire-yellow);
    background: linear-gradient(180deg, var(--fire-orange), var(--fire-red));
    color: #fff;
    box-shadow: 0 0 12px rgba(255, 100, 0, 0.35);
}
.intel-chip-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    flex: 0 0 auto;
}
.intel-chip-icon img { width: 18px; height: 18px; object-fit: contain; display: block; }
.intel-chip-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

/* --- 1C: Detail slide-over (right drawer desktop / bottom sheet mobile) -- */
.intel-detail-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
    opacity: 0;
    transition: opacity 0.2s ease;
    z-index: 5000;
}
.intel-detail-backdrop.is-open { opacity: 1; }

.intel-detail {
    position: fixed;
    top: 0;
    right: 0;
    height: 100%;
    width: min(420px, 92vw);
    max-width: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    padding: 22px 20px;
    overflow-x: hidden;
    overflow-y: auto;
    background:
        radial-gradient(circle at top right, rgba(255, 69, 0, 0.12), transparent 42%),
        linear-gradient(180deg, rgba(16, 3, 3, 0.98), rgba(9, 1, 1, 0.98));
    border-left: 1px solid var(--glass-border);
    box-shadow: -14px 0 40px rgba(0, 0, 0, 0.55);
    color: #eadfce;
    transform: translateX(100%);
    transition: transform 0.24s ease;
    z-index: 5001;
    min-width: 0;
    /* Contain the drawer's scroll so reaching its top/bottom never scrolls the
       page behind it (desktop wheel + mobile touch). */
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}
.intel-detail.is-open { transform: translateX(0); }
.intel-detail-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 34px;
    height: 34px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    border: 1px solid var(--glass-border);
    background: rgba(20, 4, 2, 0.85);
    color: #f1e6d2;
    cursor: pointer;
    transition: border-color 0.18s ease, background 0.18s ease, color 0.18s ease;
}
.intel-detail-close:hover {
    border-color: var(--fire-orange);
    color: #fff;
    background: rgba(255, 69, 0, 0.12);
}
.intel-detail-close:focus-visible { outline: 2px solid var(--fire-yellow); outline-offset: 1px; }
.intel-detail-body { min-width: 0; overflow-wrap: anywhere; }

/* Bottom sheet on small screens (spec: drawer = bottom sheet <=560px). */
@media (max-width: 560px) {
    .intel-detail {
        top: auto;
        right: 0;
        left: 0;
        bottom: 0;
        width: 100%;
        height: auto;
        max-height: 82vh;
        border-left: none;
        border-top: 1px solid var(--glass-border);
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -14px 40px rgba(0, 0, 0, 0.55);
        transform: translateY(100%);
    }
    .intel-detail.is-open { transform: translateY(0); }
    .intel-grid-sort-wrap { width: 100%; }
    .intel-grid-sort { width: 100%; max-width: none; }
}

/* Honor reduced-motion: no slide, just fade in/out via the backdrop. */
@media (prefers-reduced-motion: reduce) {
    .intel-detail { transition: none; }
    .intel-card.intel-card-clickable { transition: none; }
}
