/*
 * alignvu UI v2.1: Align > a datafeed's Database Tables step (table_connections.php) and its Dialogs (add_a_table.php,
 * create/upload/set_mapping_table.php, table_definitions.php, table_add_native_field_clientfeed.php): the tables down
 * the left, the vis-network diagram of their connections (table_connections_graph.js) and the connections list. The page is
 * scoped to .app-align-tables, the Dialogs to .app-align-tables-dialog. Some rules override legacy ones in
 * css/align_to.css that still target the classes the scripts select on (radio-check-option, downloaded-file,
 * table-def-*, checkbox-selection-option, column-selection-dropdown), noted where they do.
 */

/* v2.1 parts set display (flex, grid), which would beat the hidden attribute the scripts set */
.app-align-tables [hidden],
.app-align-tables-dialog [hidden] {
    display: none !important;
}

/* ---------- Page ---------- */

.app-align-tables {
    display: flex;
    flex-direction: column;
    gap: var(--space-5);
    padding-bottom: 32px;
    min-width: 0;
}

.app-align-tables__toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
}

.app-align-tables__actions {
    margin-left: auto;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.app-align-tables__drivers {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
}

.app-align-tables__drivers-label {
    font: 600 13px/18px var(--font-display);
    color: var(--heading-ink);
}

.app-align-tables__chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
}

.app-align-tables p.app-align-tables__status {
    color: var(--status-removed);
}

.app-align-tables__panel {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    min-width: 0;
}

.app-align-tables__count {
    color: var(--muted);
}

.app-align-tables__loading {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4) 0;
}

/* align_to.css centres the old graph's preloader over the whole panel */
.app-align-tables .connections-preloader {
    height: auto;
    width: auto;
    justify-content: flex-start;
}

/* ---------- Workspace: the tables down the left, the diagram and connections list on the right ---------- */

.app-align-tables__workspace {
    --tables-pane-height: clamp(480px, calc(100vh - 230px), 900px);
    display: grid;
    grid-template-columns: minmax(280px, 26%) minmax(0, 1fr);
    gap: var(--space-5);
    align-items: start;
}

.app-align-tables__main {
    display: flex;
    flex-direction: column;
    gap: var(--space-5);
    min-width: 0;
}

.app-align-tables__side,
.app-align-tables__diagram-panel {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    min-width: 0;
    height: var(--tables-pane-height);
}

.app-align-tables__side .av-sectionhead,
.app-align-tables__diagram-panel .av-sectionhead {
    flex: none;
}

/* ---------- Tables list (left) ---------- */

.app-align-tables__list-scroll {
    flex: 1 1 auto;
    min-height: 0;
    margin: 0 calc(-1 * var(--space-3));
    padding: 0 var(--space-3);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.app-align-tables h3.app-align-tables__group {
    position: sticky;
    top: 0;
    z-index: 1;
    margin: 0;
    padding: var(--space-3) 0 var(--space-2);
    font: 600 12px/16px var(--font-display);
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--muted);
    background: var(--surface);
    border-bottom: 1px solid var(--row-rule);
}

.app-align-tables__group-count {
    font-weight: 400;
}

.app-align-tables__list {
    margin: 0 0 var(--space-4);
    padding: 0;
    list-style: none;
}

.app-align-tables__none {
    padding: var(--space-3) 0;
    color: var(--muted);
}

.app-align-tables__item {
    padding: 3px 4px 3px 0;
    border-bottom: 1px solid var(--row-rule);
    border-left: 3px solid transparent;
}

.app-align-tables__item.is-selected {
    background: var(--day-band);
    border-left-color: var(--heading-ink);
}

.app-align-tables__item-head {
    display: flex;
    align-items: center;
    gap: 2px;
}

.app-align-tables__item-head .av-expand {
    flex: none;
}

.app-align-tables__item-name {
    flex: 1 1 auto;
    min-width: 0;
    padding: 4px 4px;
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    font: 600 13px/18px var(--font-display);
    color: var(--heading-ink);
    text-align: left;
    overflow-wrap: break-word;
    cursor: pointer;
}

.app-align-tables__item-name:hover {
    text-decoration: underline;
}

.app-align-tables__item-head .app-align-tables__actions {
    flex: none;
    margin-left: auto;
}

.app-align-tables__item-columns {
    padding: var(--space-2) 0 var(--space-2) 34px;
}

.app-align-tables__source-detail {
    margin: 0 0 var(--space-2);
    font: 400 12px/18px var(--font-display);
    color: var(--muted);
    overflow-wrap: anywhere;
}

.app-align-tables__actions-cell {
    text-align: right;
}

.app-align-tables__actions {
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-end;
    align-items: center;
    gap: 2px;
}

.app-align-tables .app-align-tables__textbtn,
.app-align-tables-dialog .app-align-tables__textbtn {
    height: 30px;
    padding: 0 10px;
    font-size: 13px;
}

.app-align-tables__actions .av-iconbtn {
    width: 28px;
    height: 28px;
}

.app-align-tables__columns {
    margin: 0;
    padding: 0 0 0 var(--space-3);
    list-style: none;
    border-left: 1px solid var(--row-rule);
}

.app-align-tables__column {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    min-height: 24px;
    padding: 2px 0;
    font: 400 12px/18px var(--font-display);
    color: var(--body-ink);
    overflow-wrap: anywhere;
}

.app-align-tables__column .av-iconbtn {
    flex: none;
    width: 22px;
    height: 22px;
}

.app-align-tables__column .av-iconbtn .av-icon {
    width: 14px;
    height: 14px;
}

/* ---------- Diagram ---------- */

.app-align-tables__diagram-tools {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    margin-left: auto;
}

.app-align-tables__diagram {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    background:
        radial-gradient(circle, var(--border) 1px, transparent 1.2px) 0 0 / 22px 22px,
        var(--paper);
    border: 1px solid var(--row-rule);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

/* align_to.css gives the old graph's container a fixed 55vh */
.app-align-tables #table-connections-container {
    height: auto;
}

.app-align-tables p.app-align-tables__hint {
    flex: none;
    margin: 0;
    font-size: 12px;
    color: var(--muted);
}

/* the empty state takes the diagram's place */
.app-align-tables__diagram-panel .app-align-tables__empty {
    flex: 1 1 auto;
}

/* ---------- Connections list (under the diagram) ---------- */

.app-align-tables__links .app-align-tables__actions-head { width: 90px; }

.app-align-tables__links th,
.app-align-tables__actions-head {
    white-space: normal;
}

.app-align-tables__actions-head {
    text-align: right;
}

/* the list scrolls on its own so the page stays short when every connection is shown */
.app-align-tables__links-panel .app-align-tables__links-frame {
    max-height: 520px;
    overflow-y: auto;
}

.app-align-tables__links-panel .app-align-tables__links thead th {
    position: sticky;
    top: 0;
    z-index: 1;
}

.app-align-tables__links tr.is-highlighted td {
    background: var(--value-tint);
}

/* ---------- Connections: two chips joined by an arrow ---------- */

.app-align-tables__filter {
    width: min(100%, 320px);
}

.app-align-tables__links td {
    vertical-align: middle;
}

.app-align-tables__link {
    display: grid;
    grid-template-columns: fit-content(46%) 52px minmax(0, max-content);
    align-items: center;
    justify-content: start;
}

.app-align-tables__chip {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: baseline;
    column-gap: 6px;
    box-sizing: border-box;
    max-width: 100%;
    min-height: 28px;
    padding: 5px 12px;
    font: 500 12.5px/16px var(--font-display);
    color: var(--heading-ink);
    background: var(--surface);
    border: 1px solid var(--muted);
    border-radius: var(--radius-pill);
    overflow-wrap: anywhere;
}

.app-align-tables__chip-table {
    font-weight: 600;
}

.app-align-tables__chip-column {
    font-weight: 400;
    color: var(--body-ink);
}

.app-align-tables__chip-column::before {
    content: "\00b7";
    margin-right: 6px;
    color: var(--muted);
}

/* the line from the first chip to the arrow head at the second */
.app-align-tables__line {
    position: relative;
    height: 1px;
    margin: 0 5px 0 0;
    background: var(--heading-ink);
}

.app-align-tables__line::after {
    content: "";
    position: absolute;
    right: -1px;
    top: -4px;
    border-left: 7px solid var(--heading-ink);
    border-top: 4.5px solid transparent;
    border-bottom: 4.5px solid transparent;
}

/* ---------- Chips with a remove button (clean drivers, standard values, header columns) ---------- */

.app-align-tables__driver {
    cursor: default;
    padding-right: 6px;
}

.app-align-tables__chip-remove {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    border: 0;
    border-radius: var(--radius-pill);
    background: none;
    color: var(--body-ink);
    cursor: pointer;
}

.app-align-tables__chip-remove:hover {
    background: var(--fill);
}

.app-align-tables__chip-remove .av-icon {
    width: 14px;
    height: 14px;
}

/* ---------- Regenerating ---------- */

.app-align-tables__regenerating {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-4);
    height: auto;
    width: auto;
    min-height: 320px;
    background: transparent;
}

.app-align-tables__regenerating .av-spinner {
    width: 32px;
    height: 32px;
    border-width: 3px;
}

.app-align-tables__regenerating h2 {
    margin: 0;
    font: 600 17px/24px var(--font-display);
    color: var(--heading-ink);
}

/* ---------- Dialogs ---------- */

.modal-content.app-align-tables-dialog {
    max-height: calc(100vh - 64px);
    display: flex;
    flex-direction: column;
}

.modal-content.app-align-tables-dialog--tall {
    height: min(820px, calc(100vh - 64px));
}

.app-align-tables-dialog__body {
    display: flex;
    flex-direction: column;
    gap: var(--space-5);
    padding: 2px;   /* room for the focus ring */
}

.app-align-tables-dialog .app-dialog__actions.app-align-tables-dialog__actions {
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
}

.app-align-tables-dialog__choices {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3) var(--space-6);
}

.app-align-tables-dialog__pair {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: var(--space-4);
}

.app-align-tables-dialog__add {
    display: flex;
    align-items: flex-end;
    gap: var(--space-3);
}

.app-align-tables-dialog__add .av-field {
    flex: 1 1 auto;
}

.app-align-tables-dialog__group {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

.app-align-tables-dialog p.app-align-tables-dialog__label,
.app-align-tables-dialog h3.app-align-tables-dialog__label,
.app-align-tables-dialog__subhead h3 {
    margin: 0;
    font: 600 13px/18px var(--font-display);
    color: var(--heading-ink);
}

.app-align-tables-dialog__note {
    color: var(--muted);
}

.app-align-tables-dialog__sql {
    height: auto;
    min-height: 160px;
    padding: 8px 12px;
    font: 400 12.5px/18px ui-monospace, Menlo, Consolas, monospace;
    resize: vertical;
}

.app-align-tables-dialog__file {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-4);
}

.app-align-tables-dialog .app-align-tables__none-text {
    color: var(--muted);
}

/* Radios and checkboxes keep the legacy classes for the scripts: undo align_to.css's boxed options and hidden inputs */
.app-align-tables-dialog .av-radio.radio-check-option,
.app-align-tables-dialog .checkbox-selection-option {
    width: auto;
    display: inline-flex;
    justify-content: flex-start;
    border: 0;
    padding: 0;
    font-weight: 400;
}

.app-align-tables-dialog .av-radio.radio-check-option:hover,
.app-align-tables-dialog .checkbox-selection-option:hover {
    background: none;
}

.app-align-tables-dialog .av-radio.radio-check-option input[type="radio"] {
    display: inline-block;
    visibility: visible;
    position: static;
    opacity: 1;
}

.app-align-tables-dialog .checkbox-selection-options {
    display: flex;
    margin: 0;
}

/* align_to.css lays the old downloaded file rows out as a grid */
.app-align-tables-dialog tr.downloaded-file {
    display: table-row;
    height: auto;
    margin: 0;
}

.app-align-tables-dialog .downloaded-files {
    max-height: 240px;
    overflow: auto;
}

.app-align-tables-dialog .downloaded-file td.filename {
    overflow-wrap: anywhere;
}

.app-align-tables-dialog .downloaded-file button.set-mapping-table-file-button {
    width: auto;
    height: 30px;
    padding: 0 12px;
    font-size: 13px;
}

.app-align-tables-dialog .downloaded-file button.set-mapping-table-file-button.active {
    background: var(--heading-ink);
    border-color: var(--heading-ink);
    color: var(--surface);
}

/* align_to.css: two-column grid and margins on these containers */
.app-align-tables-dialog .other-delimiter-container,
.app-align-tables-dialog .non-standard-eol-container {
    grid-template-columns: minmax(0, 1fr);
    margin: 0;
}

.app-align-tables-dialog .header-row-summary {
    display: inline-flex;
}

/* ---------- Table definition steps ---------- */

/* align_to.css draws each step as its own fixed-width card */
.app-align-tables-dialog .table-def-pk,
.app-align-tables-dialog .table-def-addresses,
.app-align-tables-dialog .table-def-dictionary,
.app-align-tables-dialog .table-def-relationship {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    flex: 1 1 auto;
    min-height: 0;
    width: auto;
    max-height: none;
    padding: 0;
    overflow: visible;
    box-shadow: none;
    color: inherit;
    background: none;
}

.app-align-tables-dialog .table-def-related-key {
    padding-top: var(--space-4);
    border-top: 1px solid var(--row-rule);
}

.app-align-tables-dialog p.app-align-tables-def__stepname {
    margin: -8px 0 0;
    font: 500 12.5px/18px var(--font-display);
    color: var(--muted);
}

.app-align-tables-def__selects {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: var(--space-3);
}

.app-align-tables-def__selects:empty {
    display: none;
}

.app-align-tables-def__add {
    margin-top: -8px;
}

.app-align-tables-def__add .av-btn {
    height: 30px;
    padding: 0 8px;
    font-size: 13px;
}

.app-align-tables-def__distinctions {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    padding: var(--space-4);
    background: var(--day-band);
    border-radius: var(--radius-lg);
}

.app-align-tables-def__distinction {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

/* align_to.css: two-column grid, small text and margins on the old distinction inputs */
.app-align-tables-dialog .entity-dist-comp-value.app-align-tables-def__distinction-row {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(0, 1.3fr) minmax(0, 2fr);
    gap: var(--space-3);
}

.app-align-tables-dialog .column-selection-dropdown.av-select,
.app-align-tables-dialog .table-def-entity-value.av-input {
    margin: 0;
    font-size: 14px;
    line-height: 20px;
}

/* ---------- Narrow windows ---------- */

@media (max-width: 1200px) {
    .app-align-tables__workspace {
        grid-template-columns: minmax(260px, 32%) minmax(0, 1fr);
        gap: var(--space-4);
    }
}

/* one column: the tables list above the diagram, each a fixed height */
@media (max-width: 900px) {
    .app-align-tables__workspace {
        grid-template-columns: minmax(0, 1fr);
    }

    .app-align-tables__side {
        height: auto;
        max-height: 420px;
    }
}

@media (max-width: 720px) {
    .app-align-tables-dialog .entity-dist-comp-value.app-align-tables-def__distinction-row {
        grid-template-columns: minmax(0, 1fr);
    }
}

/* ---------- Legacy overrides for the Dialogs ---------- */

/* align_to.css gives these old form wrappers a fixed 647px card */
.app-align-tables-dialog.add-table,
.app-align-tables-dialog .create-mapping-table,
.app-align-tables-dialog .upload-mapping-table,
.app-align-tables-dialog .set-mapping-table {
    width: auto;
    max-height: none;
    overflow-x: hidden;
    overflow-y: auto;
    box-shadow: none;
    border-radius: 0;
    background: none;
}

.app-align-tables-dialog.add-table {
    width: min(480px, calc(100vw - 32px));
    overflow: hidden;
    background: var(--surface);
}

/* the Dialog body scrolls; its parts keep their height instead of shrinking into each other */
.app-align-tables-dialog__body > * {
    flex-shrink: 0;
}

/* align_to.css: text colour and margins on paragraphs in the old forms */
.app-align-tables-dialog p.av-field__error {
    margin: 0;
    font: 600 12px/18px var(--font-display);
    color: var(--status-removed);
}

/* v21_app.css lets a FeedTable in a Dialog grow; the downloaded files list scrolls on its own */
.app-align-tables-dialog .modal-body .av-table__frame.downloaded-files {
    max-height: 240px;
    overflow: auto;
}

/* ---------- Data quality Dialogs (application/views/configuration/configure/data_quality.php), laid out like
   Connect's schedule audit (css/v21_connect_schedules.css): a wide Dialog whose body scrolls, a DetailList, Tabs
   and FeedTables; the column coverage Dialog reuses the audit field detail's tiles and count tables ---------- */

.modal-content.app-dialog.app-align-dq {
    width: min(1240px, calc(100vw - 32px));
    height: min(860px, calc(100vh - 48px));
}

/* (the id beats the old 800px, full height rule in css/align_to.css) */
#data-quality-column-coverage-modal .modal-content.app-dialog.app-align-dq-column {
    width: min(760px, calc(100vw - 32px));
    height: auto;
    max-height: calc(100vh - 48px);
}

#data-quality-primary-modal .modal-content.app-dialog .modal-body,
#data-quality-column-coverage-modal .modal-content.app-dialog .modal-body {
    display: flex;
    flex-direction: column;
    gap: 16px;
    flex: 1 1 auto;
    max-height: none;
    overflow: auto;
}

.app-align-dq__title {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: 4px;
    row-gap: 6px;
}

.app-align-dq__pills {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-left: 12px;
}

.app-align-dq__pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    background: var(--day-band);
    font: 500 12.5px/18px var(--font-display);
    color: var(--heading-ink);
}

.app-align-dq__pill:empty,
.app-align-dq__pill--column:has(.data-quality-column-name:empty) {
    display: none;
}

.app-align-dq__content {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-height: 0;
    flex: 1 1 auto;
}

#data-quality-primary-modal [hidden],
#data-quality-column-coverage-modal [hidden] {
    display: none;
}

.app-align-dq__details {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

.app-align-dq__details dd.av-num {
    text-align: left;
    font-variant-numeric: tabular-nums;
}

.app-align-dq__tabs,
.app-align-dq-column__tabs {
    flex: none;
    overflow-x: auto;
}

.app-align-dq__tabs .av-tab,
.app-align-dq-column__tabs .av-tab {
    flex: none;
    white-space: nowrap;
}

.app-align-dq__tab-count {
    font-weight: 400;
    color: var(--muted);
}

.app-align-dq__panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 0;
}

/* the sample's filter: Rows, then a condition (field, comparator, value) and its buttons on one line that wraps */
.app-align-dq__filters {
    display: grid;
    grid-template-columns: 96px repeat(3, minmax(140px, 1fr)) auto;
    align-items: end;
    gap: 12px;
}

.app-align-dq__filters .av-field {
    min-width: 0;
}

.app-align-dq__filter-actions {
    flex-wrap: nowrap;
}

.app-align-dq__conditions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
}

.app-align-dq__condition {
    cursor: default;
    overflow-wrap: anywhere;
}

/* the sample: headings stay at the top, cells wrap (line breaks kept) with a sensible width */
.app-align-dq__sample-frame {
    max-height: 520px;
    overflow: auto;
}

.app-align-dq__sample th,
.app-align-dq__sample td {
    min-width: 120px;
    max-width: 260px;
    vertical-align: top;
    white-space: nowrap;   /* one line per row: long text ends in an ellipsis, the full text is its tooltip */
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-align-dq__sample thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    white-space: nowrap;
}

.app-align-dq__derived {
    margin-left: 4px;
    padding: 0 6px;
    font-size: 11px;
}

.app-align-dq__columns .app-align-dq__seq {
    width: 1%;
}

.app-align-dq__columns .app-align-dq__column-row {
    cursor: pointer;
}

/* a column's name (and a coverage count's label) is a plain-looking button for the keyboard */
.app-align-dq__column-link {
    padding: 0;
    border: 0;
    background: none;
    font: inherit;
    font-weight: 600;
    color: var(--heading-ink);
    text-align: left;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    cursor: pointer;
}

.app-align-dq__column-link:hover {
    text-decoration: underline;
}

.app-align-dq__column-link:focus-visible {
    outline: 2px solid var(--heading-ink);
    outline-offset: 2px;
}

.app-align-dq-column__counts .app-align-dq__column-link {
    font-weight: 400;
    color: var(--table-ink);
}

.app-align-dq-column__share {
    font: 400 13px/18px var(--font-display);
    color: var(--muted);
}

/* the Add clean driver Dialog: the chosen driver's description under the drop-down */
#table-connections-clean-driver-help {
    margin: 4px 0 0;
    font: 400 13px/18px var(--font-display);
    color: var(--muted);
}

.app-align-dq__actions {
    justify-content: space-between;
}

/* narrow: the condition's three inputs share the line with Rows, and the buttons go under them */
@media (max-width: 1180px) {
    .app-align-dq__filters {
        grid-template-columns: 80px repeat(3, minmax(0, 1fr));
    }

    .app-align-dq__filter-actions {
        grid-column: 1 / -1;
    }
}
