/* SQL Query Builder (/sql-query-builder): what is its own (the clause rows of the builder, the coloured SQL, the result
   table and the list of tables). Panels, fields, buttons, tables and colours come from the app kit (css/app-kit,
   docs/app-kit.md). Loaded only on this app's page (StylePaths in its demo catalog entry). */

/* On a phone the site's photo-app rule (seo-pages.css) sets align-items: start on every stage, which beats the kit's
   stretch: the panels would shrink to their own width. */
.sqb-landing-app > .ak-stage > * {
    align-self: stretch;
}

/* The builder and the SQL side by side on very wide screens, else one above the other (the builder rows then fit on one line). */
.sqb-landing-app .sqb-top {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--ak-space-4);
    align-items: start;
}

@media (min-width: 1700px) {
    .sqb-landing-app .sqb-top {
        grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr);
    }

    .sqb-landing-app .sqb-sql-panel {
        position: sticky;
        top: var(--ak-space-4);
    }
}

/* --- The builder: one row per clause, the keyword on the left ---------------------------------------------------- */

.sqb-landing-app .sqb-builder {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
}

.sqb-landing-app .sqb-clause {
    display: grid;
    grid-template-columns: 7.4rem minmax(0, 1fr);
    gap: 0.35rem 0.6rem;
    align-items: start;
    padding-bottom: 0.45rem;
    border-bottom: 1px solid var(--ak-grid);
}

.sqb-landing-app .sqb-clause:last-child {
    padding-bottom: 0;
    border-bottom: 0;
}

.sqb-landing-app .sqb-kw,
.sqb-landing-app .sqb-word {
    font-family: var(--ak-font-mono);
    font-size: var(--ak-fs-sm);
    font-weight: 700;
    color: var(--ak-accent);
    white-space: nowrap;
}

/* The keyword and its + button. */
.sqb-landing-app .sqb-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.3rem;
    min-height: 2rem;
    direction: ltr;
}

.sqb-landing-app .sqb-clause--empty .sqb-items {
    display: none;
}

.sqb-landing-app .sqb-word {
    color: var(--ak-muted);
    font-weight: 600;
}

.sqb-landing-app .sqb-items {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.35rem;
    min-width: 0;
}

.sqb-landing-app .sqb-item {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.3rem;
    width: 100%;
    min-width: 0;
}

/* Compact fields in a row: each takes the room its content needs, and they wrap on narrow screens. The input selector
   matches the kit's long one for text inputs (components.css), so these sizes win. */
.sqb-landing-app .sqb-item select.ak-field,
.sqb-landing-app.ak-app .sqb-builder .sqb-item input.ak-field.sqb-input[type="text"] {
    width: auto;
    min-width: 0;
    min-height: 2rem;
    padding: 0.25rem 0.45rem;
    font-family: var(--ak-font-mono);
    font-size: 0.8rem;
    line-height: 1.3;
}

.sqb-landing-app .sqb-item .sqb-sel--table { flex: 0 1 auto; max-width: 10rem; }
.sqb-landing-app .sqb-item .sqb-sel--col { flex: 0 1 auto; max-width: 9.5rem; }
.sqb-landing-app .sqb-item .sqb-sel--kw { flex: 0 1 auto; max-width: 7.5rem; }
.sqb-landing-app .sqb-item .sqb-sel--op { flex: 0 1 auto; max-width: 7.5rem; }
.sqb-landing-app .sqb-item .sqb-sel--conj { flex: 0 0 4.2rem; width: 4.2rem; }
.sqb-landing-app.ak-app .sqb-builder .sqb-item input.sqb-input--alias[type="text"] { flex: 0 1 6.5rem; width: 6.5rem; }
.sqb-landing-app.ak-app .sqb-builder .sqb-item input.sqb-input--val[type="text"] { flex: 0 1 7.5rem; width: 7.5rem; }
.sqb-landing-app.ak-app .sqb-builder .sqb-item input.sqb-input--num[type="text"] { flex: 0 1 5rem; width: 5rem; }

.sqb-landing-app .sqb-check {
    gap: 0.4rem;
    cursor: pointer;
}

.sqb-landing-app .sqb-check input {
    width: 1rem;
    height: 1rem;
    margin: 0;
    accent-color: var(--ak-accent);
}

/* The small + and x buttons of the rows: quiet, not the accent fill the science apps give every stage button. */
.sqb-landing-app.ak-app > .filter-landing-stage .sqb-builder .ak-btn.sqb-icon-btn {
    flex: none;
    border-color: transparent;
    background: transparent;
    color: var(--ak-muted);
}

.sqb-landing-app.ak-app > .filter-landing-stage .sqb-builder .ak-btn.sqb-icon-btn:hover:not(:disabled) {
    border-color: transparent;
    background: var(--ak-hover);
    color: var(--ak-danger);
}

.sqb-landing-app.ak-app > .filter-landing-stage .sqb-builder .ak-btn.sqb-add {
    border: 1px dashed color-mix(in srgb, var(--ak-accent) 55%, var(--ak-border));
    color: var(--ak-accent);
}

.sqb-landing-app.ak-app > .filter-landing-stage .sqb-builder .ak-btn.sqb-add:hover:not(:disabled) {
    border-color: var(--ak-accent);
    background: color-mix(in srgb, var(--ak-accent) 10%, transparent);
    color: var(--ak-accent);
}

.sqb-landing-app .sqb-icon-btn .ak-icon {
    width: 1rem;
    height: 1rem;
}

/* --- The SQL --------------------------------------------------------------------------------------------------------- */

.sqb-landing-app .sqb-sql-panel .ak-panel-head {
    justify-content: flex-end;
}

.sqb-landing-app .sqb-code {
    margin: 0;
    padding: 0.7rem 0.85rem;
    min-height: 6rem;
    max-height: 32rem;
    overflow: auto;
    border: 1px solid var(--ak-border);
    border-radius: var(--ak-radius-sm);
    background: var(--ak-field);
    color: var(--ak-text);
    font-family: var(--ak-font-mono);
    font-size: 0.85rem;
    line-height: 1.55;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    direction: ltr;
    text-align: left;
    tab-size: 4;
}

.sqb-landing-app .sqb-code:focus-visible {
    outline: 2px solid var(--ak-accent);
    outline-offset: 1px;
}

.sqb-landing-app .sqb-t-k { color: var(--ak-accent); font-weight: 700; }
.sqb-landing-app .sqb-t-f { color: var(--ak-info); font-weight: 600; }
.sqb-landing-app .sqb-t-s { color: var(--ak-success); }
.sqb-landing-app .sqb-t-n { color: var(--ak-warning); }
.sqb-landing-app .sqb-t-p { color: var(--ak-muted); }
.sqb-landing-app .sqb-t-i { color: var(--ak-text); }
.sqb-landing-app .sqb-t-x {
    color: var(--ak-danger);
    text-decoration: underline wavy;
    text-underline-offset: 3px;
}

.sqb-landing-app .sqb-message {
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
}

.sqb-landing-app .sqb-message > span {
    flex: 1 1 12rem;
}

.sqb-landing-app .sqb-message .ak-btn {
    margin-inline-start: auto;
}

/* --- The result ------------------------------------------------------------------------------------------------------ */

.sqb-landing-app .sqb-result-head {
    justify-content: flex-start;
}

.sqb-landing-app .sqb-status {
    flex: 1 1 12rem;
    margin: 0;
    color: var(--ak-muted);
    font-size: var(--ak-fs-sm);
    font-variant-numeric: tabular-nums;
}

.sqb-landing-app .sqb-result.is-stale .sqb-table-wrap {
    opacity: 0.5;
}

.sqb-landing-app .sqb-result.is-stale .sqb-status {
    color: var(--ak-warning);
}

.sqb-landing-app .sqb-table-wrap {
    max-height: 30rem;
    overflow: auto;
}

.sqb-landing-app .sqb-table {
    font-size: var(--ak-fs-sm);
}

.sqb-landing-app .sqb-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--ak-panel);
    font-family: var(--ak-font-mono);
    white-space: nowrap;
}

.sqb-landing-app .sqb-table td {
    max-width: 22rem;
    overflow-wrap: anywhere;
}

.sqb-landing-app .sqb-table td.ak-num {
    white-space: nowrap;
}

.sqb-landing-app .sqb-null {
    color: var(--ak-muted);
    font-style: italic;
    font-size: 0.75rem;
}

/* --- The tables in the side panel ------------------------------------------------------------------------------------ */

.sqb-landing-app .sqb-schema {
    display: flex;
    flex-direction: column;
    border: 1px solid var(--ak-border);
    border-radius: var(--ak-radius-sm);
    background: var(--ak-field);
    overflow: hidden;
}

.sqb-landing-app .sqb-schema-table + .sqb-schema-table {
    border-top: 1px solid var(--ak-grid);
}

.sqb-landing-app .sqb-schema-head {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-height: 2.1rem;
    padding: 0.2rem 0.6rem;
    cursor: pointer;
    list-style: none;
    font-size: var(--ak-fs-sm);
}

.sqb-landing-app .sqb-schema-head::-webkit-details-marker {
    display: none;
}

.sqb-landing-app .sqb-schema-head::before {
    content: "";
    flex: none;
    width: 0.4rem;
    height: 0.4rem;
    border-right: 2px solid var(--ak-muted);
    border-bottom: 2px solid var(--ak-muted);
    transform: rotate(-45deg);
    transition: transform var(--ak-duration) var(--ak-ease);
}

[dir="rtl"] .sqb-landing-app .sqb-schema-head::before {
    transform: rotate(135deg);
}

.sqb-landing-app .sqb-schema-table[open] > .sqb-schema-head::before {
    transform: rotate(45deg);
}

.sqb-landing-app .sqb-schema-name {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    font-family: var(--ak-font-mono);
    font-weight: 600;
}

.sqb-landing-app .sqb-schema-count {
    flex: none;
    color: var(--ak-muted);
    font-size: var(--ak-fs-xs);
}

.sqb-landing-app .sqb-schema-cols {
    margin: 0;
    padding: 0 0.6rem 0.45rem 1.5rem;
    list-style: none;
    font-family: var(--ak-font-mono);
    font-size: 0.78rem;
}

.sqb-landing-app .sqb-schema-cols li {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.1rem 0;
}

.sqb-landing-app .sqb-schema-col {
    min-width: 0;
    overflow-wrap: anywhere;
}

.sqb-landing-app .sqb-schema-type {
    flex: none;
    color: var(--ak-muted);
}

.sqb-landing-app .sqb-import-msg {
    margin: 0;
}

/* --- Phones: the keyword above its fields ---------------------------------------------------------------------------- */

@media (max-width: 640px) {
    .sqb-landing-app .sqb-clause {
        grid-template-columns: minmax(0, 1fr);
        gap: 0.25rem;
    }

    .sqb-landing-app .sqb-head {
        justify-content: flex-start;
        gap: 0.6rem;
    }

    .sqb-landing-app .sqb-item .sqb-sel--col {
        flex: 1 1 7rem;
        max-width: none;
    }

    .sqb-landing-app.ak-app .sqb-builder .sqb-item input.sqb-input--val[type="text"],
    .sqb-landing-app.ak-app .sqb-builder .sqb-item input.sqb-input--alias[type="text"] {
        flex: 1 1 6rem;
    }

    .sqb-landing-app .sqb-code {
        font-size: 0.8rem;
    }
}
