/*
 * Panel-wide adjustments to Unfold, on every admin page. It is linked BEFORE Unfold's own stylesheet (the skeleton
 * prints UNFOLD['STYLES'] ahead of its RTL block), so a rule here wins by being more specific than the rule it
 * corrects, never by coming later (utils/tests/test_admin_templates.py, StylesheetOrderTests).
 */

/*
 * Autocomplete boxes in table rows (e.g. the product of a proforma row, the currency of a product price).
 * Unfold makes them as wide as their cell, but nothing gives the cell a width of its own: with nothing
 * selected yet (a new document or an added row) the table shrank the column to its buttons and the box to a
 * sliver, too narrow to see or pick an item. The minimum width keeps the box and its list usable; wider screens
 * give the column more. It is small enough for the proforma products table to fit a 1280px screen with the
 * sidebar open (and a Windows scrollbar), without scrolling sideways.
 */
@media (min-width: 1024px) {
    /* Unfold shows table rows as stacked cards below this width, where the box takes the card's width instead */
    .tabular-table .related-widget-wrapper .select2.select2-container {
        min-width: 14rem;
    }
}

/*
 * Autocomplete boxes next to their buttons (edit, add, view, delete): Unfold makes the box as wide as the field, but
 * the text of the item picked set the least width of the box and of everything around it: on a phone a long name
 * and four buttons pushed the form wider than the screen, and in a table a long product name widened its column.
 * The box now fills what its buttons leave (in a table no less than the minimum above) and cuts a long name.
 * Unfold's own rule (`.select2.select2-container`, `width: 100% !important`) has two classes; this one names both
 * of them and the wrapper, so it wins wherever the two stylesheets are linked.
 */
.related-widget-wrapper .select2.select2-container {
    flex: 1 1 0%;
    min-width: 0;
    width: 0 !important;
}

/*
 * The text of an autocomplete box stops before the dropdown arrow (in a narrow box it ran under it), and a long
 * name is cut at its own end: a Latin product name keeps its start («Melon Ariana (Enza Z…»), a Persian one too.
 */
[dir="rtl"] .select2.select2-container--admin-autocomplete .select2-selection--single .select2-selection__rendered {
    padding-inline-end: 2.25rem;
    text-align: right;
    unicode-bidi: plaintext;
}

/*
 * Form sections: a <fieldset> is never narrower than its widest content by default, so on a phone a long file name
 * (e.g. «proforma_files/proforma-1497.pdf») or a long autocomplete item made the whole form wider than the screen.
 * Unfold already lifts this for its tables (min-w-0); the form's own sections now fit the screen too, and those
 * names are cut with «…» inside their boxes.
 */
fieldset.module {
    min-width: 0;
}

/*
 * Plain dropdowns in table rows (e.g. the unit of a proforma row): the same squeeze left them too narrow for
 * their arrow and text, so «انتخاب مقدار» read «انتخاب م…». The minimum width fits the arrow and a short label.
 */
.tabular-table td select:not(.select2-hidden-accessible) {
    min-width: 7.5rem;
}

/*
 * Number boxes in table rows (e.g. the amount and unit price of a proforma row): at Unfold's 5rem, after its padding
 * and the spin arrows, four or five characters showed, so «60.00» read «.00» and «500000.00» «00.00». Without the
 * arrows (numbers are typed) and with less padding the box shows eight characters, and the amount (the largest
 * numbers, e.g. seed counts) gets room for ten. Written left to right, a number too long still shows its start.
 * The amounts, weights and prices are the panel's amount box (a text box that reads Persian digits), found by its
 * class; the remaining number boxes (e.g. an order) by their type.
 */
.tabular-table td input[type="number"],
.tabular-table td input.decimal-input {
    -moz-appearance: textfield;
    appearance: textfield;
    direction: ltr;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    text-align: right;
}

.tabular-table td input[type="number"]::-webkit-inner-spin-button,
.tabular-table td input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.tabular-table td.field-amount input {
    min-width: 6.5rem;
}

/* An amount box elsewhere on a form also reads left to right, and keeps to the right edge like the boxes beside it */
input.decimal-input {
    text-align: right;
}

/* Boxes that cannot be changed in a table row (e.g. a product price recorded from a proforma) look it */
.tabular-table input:disabled,
.tabular-table select:disabled,
.tabular-table .select2.select2-container--disabled,
.tabular-table .select2-container--disabled .select2-selection {
    background-color: var(--color-base-100);
    color: var(--color-base-500);
    cursor: not-allowed;
}

/*
 * Page tabs (templates/unfold/helpers/tab_list.html): the action buttons get a row of their own, at the end of the
 * line, and the tabs a full row under them. Unfold put both on one line, so a page with many tabs squeezed the
 * tab titles onto two lines and pushed the buttons off narrower screens. Tab titles stay on one line; tabs that
 * do not fit move to the next line.
 */
.tab-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.tab-list > ul {
    align-self: flex-end;
    flex-wrap: wrap;
    margin: 0;
    max-width: 100%;
}

.tab-list > nav {
    flex-wrap: wrap;
    width: 100%;
}

.tab-list > nav > a,
.tab-list > ul a {
    white-space: nowrap;
}

/* The fieldset tabs of a form (e.g. «پروفرم»، «ثبت سفارش») likewise wrap instead of running off narrow screens */
ul:has(+ .tab-wrapper) {
    flex-wrap: wrap;
}

ul:has(+ .tab-wrapper) > li > a {
    white-space: nowrap;
}

/*
 * A red dot on the tabs and sub-tabs of a form that hold an error (form_position.js): with many tabs, the error of
 * a page sent back was hard to find.
 */
.tab-has-errors::after {
    background: var(--color-red-600);
    border-radius: 9999px;
    content: '';
    display: inline-block;
    height: 0.45rem;
    margin-inline-start: 0.4rem;
    vertical-align: middle;
    width: 0.45rem;
}

/*
 * The two lists of a many-to-many picker (a role's «اجازه‌ها», a user's groups) are a fixed 24rem each in Unfold, so
 * beside the sidebar and the label column the second list ran off the edge of a laptop screen. They share the width
 * of the row instead; below md they stack as before.
 */
@media (min-width: 768px) {
    .selector .selector-available,
    .selector .selector-chosen {
        flex: 1 1 0;
        min-width: 0;
        width: auto;
    }
}
