/* ── Generic table + item-list-table + table cell/header components ──────── */

table {
    min-width: var(--control-width);
    width: auto;
    max-width: 100%;
    border-collapse: collapse;
    color: var(--text-main);
    background-color: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: var(--space-4);
}

th {
    background-color: var(--bg-hover);
    color: var(--text-muted);
    font-weight: 600;
    text-align: left;
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

td {
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

tbody tr:hover td {
    background-color: var(--bg-hover);
}

.item-list-table-wrapper {
    max-height: 320px;
    overflow-y: auto;
}

.item-list-table-wrapper table {
    margin-bottom: 0;
}

.item-list-table thead th {
    position: sticky;
    top: 0;
    z-index: 1;
}

.col-serial .copyable-cell {
    max-width: 200px;
    white-space: normal;
    word-break: break-word;
}

.col-name .copyable-cell {
    max-width: 450px;
    white-space: normal;
    word-break: break-word;
}

/* ── Alerts column (note / user-comment markers) ────────────────────────── */
/* The column carries no label and collapses to zero width when every row has
   no markers; when a marker is present the table layout widens it to fit the
   icons (width: 0 is only the preferred width — content wins the minimum). */

th.col-alerts,
td.col-alerts {
    width: 0;
    padding: 0;
    overflow: visible;
}

th.col-alerts .th-content {
    padding: 0;
}

.alert-icons {
    display: inline-flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--space-1);
    padding: 0 var(--space-2);
    white-space: nowrap;
}

.alert-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 1rem;
    height: 1rem;
    color: var(--text-muted);
    cursor: help;
}

.alert-icon svg {
    width: 100%;
    height: 100%;
    display: block;
}

.alert-icon:hover,
.alert-icon:focus-visible {
    color: var(--primary);
}

/* The alerts column is always the last one, so anchor the tooltip to the cell
   right edge (grows leftward) instead of centering under the icon — otherwise
   it would overflow the table wrapper and get clipped.  No pre-wrap here:
   each note/comment renders as its own block line, so preserved whitespace
   would add empty line boxes between them (the newline + indentation text
   nodes around the spans) and inflate the tooltip height. */

.alert-icon .help-tooltip {
    left: auto;
    right: 0;
    transform: none;
    width: max-content;
    max-width: min(22rem, calc(100vw - var(--space-4)));
    word-break: break-word;
    overflow-wrap: break-word;
}

/* Multiple user comments inside the bubble tooltip: one line each, separated
   by a thin divider instead of a bare line break. */

.alert-icon .help-tooltip .alert-tooltip-line {
    display: block;
}

.alert-icon .help-tooltip .alert-tooltip-line + .alert-tooltip-line {
    border-top: 1px solid var(--border-color);
    margin-top: var(--space-1);
    padding-top: var(--space-1);
}

/* ── Scrollable table wrapper (sticky thead) ─────────────────────────────── */

.table-wrapper {
    max-height: calc(100vh - 280px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.table-wrapper table {
    margin-bottom: 0;
}

.table-wrapper thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--bg-hover);
}

.table-wrapper thead th.sortable-header:hover,
.table-wrapper thead th.sortable-header:focus-within {
    z-index: 3;
}

/* ── Sortable column headers (interactive item tables) ──────────────────── */

.sortable-header {
    cursor: default;
    user-select: none;
    overflow: visible;
    padding: 0;
}

.th-content {
    display: flex;
    align-items: center;
    padding: var(--space-2) var(--space-3);
}

.th-label {
    position: relative;
    display: inline-block;
    white-space: nowrap;
}

.th-tools {
    position: absolute;
    top: 50%;
    left: 100%;
    transform: translateY(-50%);
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
    gap: 0;
    padding: var(--space-1);
    width: max-content;
    border-radius: var(--radius);
    background: var(--bg-hover);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition);
    z-index: 4;
}

/* Last header that actually carries tools: left:100% pushes them past the table
   edge, and .table-wrapper turns that into horizontal scrollable overflow
   (hidden tools still count — they are only opacity:0, not display:none).
   Anchor them to the right edge of the cell instead. */
.sortable-header:has(.th-tools):not(:has(~ th .th-tools)) .th-label {
    position: static;
}

.sortable-header:has(.th-tools):not(:has(~ th .th-tools)) .th-tools {
    left: auto;
    right: 0;
}

.sortable-header:hover .th-tools,
.sortable-header:focus-within .th-tools,
.th-tools:hover {
    opacity: 1;
    pointer-events: auto;
}

.sortable-header:has(.sort-active-asc, .sort-active-desc, .filter-active) .th-tools {
    position: static;
    transform: none;
    width: max-content;
    opacity: 1;
    pointer-events: auto;
}

@media (hover: none) {
    .th-tools {
        position: static;
        transform: none;
        width: max-content;
        opacity: 1;
        pointer-events: auto;
    }
}

.sort-btn,
.col-filter-toggle,
.copy-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.4rem;
    height: 1.4rem;
    padding: 0;
    border: none;
    border-radius: var(--radius);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.7rem;
    line-height: 1;
    flex-shrink: 0;
    transition: background var(--transition), color var(--transition);
}

.sort-btn::after {
    content: '⇅';
    font-size: 0.85rem;
    opacity: 0.5;
}

.sort-btn:hover,
.col-filter-toggle:hover,
.copy-btn:hover {
    background: var(--bg-accent);
    color: var(--text-main);
}

.sort-btn.sort-active-asc::after {
    content: '▲';
    opacity: 1;
    color: var(--primary);
}

.sort-btn.sort-active-desc::after {
    content: '▼';
    opacity: 1;
    color: var(--primary);
}

.col-filter-toggle::after {
    content: '⏍';
    font-size: 0.9rem;
    opacity: 0.5;
}

.col-filter-toggle.filter-active::after {
    opacity: 1;
    color: var(--primary);
}

.copy-btn svg {
    width: 0.9rem;
    height: 0.9rem;
    display: block;
    opacity: 0.5;
}

.copy-btn:hover svg {
    opacity: 1;
}

.copy-btn.filter-active svg {
    opacity: 1;
    color: var(--primary);
}

.col-filter-input-wrapper {
    margin-top: calc(-1 * var(--space-2));
    padding: 0 var(--space-3) var(--space-3);
}

.col-filter-input {
    width: 100%;
    min-width: 6rem;
    padding: var(--space-1) var(--space-2);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-accent);
    color: var(--text-main);
    font-size: 0.8rem;
    font-family: inherit;
}

.col-filter-input:focus {
    outline: none;
    border-color: var(--primary);
}

/* ── Row selection cells (catalog find) ─────────────────────────────────── */

.selection-header,
.selection-cell {
    white-space: nowrap;
}

.selection-control {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 2.2rem;
}

/* The sizer mirrors the display text (synced by catalog-selection) and is the
   only in-flow child, so the control sizes to its content: the column takes
   the width of its widest element and every control stretches to that width.
   The 1px padding is the deliberate "hug the text" exception to the scale. */
.selection-sizer {
    visibility: hidden;
    white-space: nowrap;
    font: inherit;
    letter-spacing: inherit;
    padding-inline: 1px;
}

.selection-display,
.selection-input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: auto;
    max-width: none;
    margin: 0;
    padding: 0 1px;
    text-align: center;
}

.selection-display[readonly] {
    cursor: pointer;
}

.page-selection .selection-display,
.page-selection .selection-input {
    background-color: var(--bg-hover);
    font-weight: 600;
}

/* ── Copyable cells ─────────────────────────────────────────────────────── */

.copyable-cell {
    position: relative;
}

.field-link {
    color: var(--text-main);
    text-decoration: none;
    transition: color var(--transition);
}

.copy-field-btn {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: none;
    outline: none;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    background-color: var(--bg-hover);
    cursor: pointer;
    opacity: 0;
    transition: opacity var(--transition);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23379814' d='M16 1H4c-1.1 0-2 .9-2 2v12h2V3h12V1z'/%3E%3Cpath fill='%23379814' d='M20 5H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h12v14z'/%3E%3C/svg%3E");
    position: absolute;
    z-index: 1;
    left: 0;
    top: 0;
}

.copy-field-btn:focus,
.copy-field-btn:focus-visible,
.copy-field-btn:active {
    outline: none;
    box-shadow: none;
}

.copy-field-btn::before {
    content: '';
    position: absolute;
    top: var(--grad-top, 0);
    right: 100%;
    width: 100px;
    height: var(--grad-height, 100%);
    background: linear-gradient(to right, rgba(47, 47, 47, 0), var(--bg-hover));
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition);
    z-index: -1;
}

@media (hover: hover) {
    .field-link:hover {
        color: var(--text-main);
    }

    .copyable-cell:hover .copy-field-btn {
        opacity: 1;
    }

    .copyable-cell:hover .copy-field-btn::before,
    .copy-field-btn:hover::before {
        opacity: 1;
    }
}

@media (hover: none) {
    .copy-field-btn {
        display: none;
    }
}

.mixed-value {
    display: inline-flex;
    align-items: center;
    padding: 0 var(--space-1);
    border-radius: 999px;
    border: 1px solid var(--warning-border);
    background-color: var(--warning-bg-subtle);
    color: var(--warning-text);
    font-weight: 600;
}

.stack-row td {
    background-color: var(--primary-bg-faint);
}

.stack-summary-actions form {
    max-width: none;
}

.select2-container--default .select2-results__option--selected {
    background-color: var(--bg-accent);
}
