/*
 * Shared compact form rhythm.
 *
 * Module styles own colours and layout semantics; this file owns density so
 * CRM, ERP, OA and System forms do not drift back to oversized spacing.
 */

:root {
    --form-control-height: 36px;
    --form-field-gap: 4px;
    --form-grid-row-gap: 8px;
    --form-grid-column-gap: 10px;
    --form-section-gap: 8px;
    --form-section-padding: 10px;
}

/*
 * Compact list filters are a shared interaction contract.
 *
 * Page styles still decide the exact number and width of columns.  Keeping the
 * layout primitive here prevents a page from silently falling back to stacked
 * browser blocks when a module stylesheet only supplies grid-template-columns.
 */
:where(.filter-form) {
    display: grid;
    align-items: end;
    gap: 12px;
    min-width: 0;
}

:where(.filter-form > label) {
    display: grid;
    min-width: 0;
    gap: 4px;
    color: var(--text-soft);
    font-size: 12px;
    font-weight: 800;
}

:where(.filter-form .filter-actions) {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: max-content;
    white-space: nowrap;
}

:where(.filter-form .filter-actions > *) {
    flex: 0 0 auto;
    white-space: nowrap;
}

/*
 * Safe form fallback.
 *
 * Module-specific rules remain the primary design layer. These zero-specificity
 * selectors only prevent a newly added or legacy control from falling back to
 * the browser's native bevelled button / unstyled input when its parent class
 * was omitted. Any class selector in a module stylesheet still wins.
 */
:where(.content input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]):not([type="file"])),
:where(.content select),
:where(.content textarea) {
    width: 100%;
    min-width: 0;
    min-height: var(--form-control-height);
    padding: 7px 10px;
    color: var(--text);
    background: #fff;
    border: 1px solid #c9ddd2;
    border-radius: 6px;
    font: inherit;
    line-height: 1.35;
    transition: border-color .16s ease, box-shadow .16s ease, background .16s ease;
}

:where(.content textarea) {
    min-height: 64px;
    resize: vertical;
}

:where(.content input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]):not([type="file"]):focus),
:where(.content select:focus),
:where(.content textarea:focus) {
    border-color: var(--green-700);
    outline: 0;
    box-shadow: 0 0 0 3px rgba(11, 107, 79, .10);
}

:where(.content input[readonly]),
:where(.content input:disabled),
:where(.content select:disabled),
:where(.content textarea:disabled) {
    color: #65766f;
    background: #f3f7f5;
    cursor: not-allowed;
}

/* Native single-selects inside data tables use one compact visual contract.
 * The zero-specificity selector supplies a safe default without overriding
 * deliberate page components, searchable selects, or multi-select fields. */
:where(.content .data-table select:not([multiple]):not(.kst-searchable-native)) {
    height: var(--ui-table-control-height);
    min-height: var(--ui-table-control-height);
    padding: 0 28px 0 10px;
    background-color: #fff;
    border-color: var(--ui-field-border);
    border-radius: var(--ui-field-radius);
    line-height: normal;
}

/* Searchable customer and product selectors. The native select stays in the
 * form for browser validation and server-compatible submission; the visible
 * combobox only improves discovery and never broadens the server queryset. */
.kst-searchable-select {
    position: relative;
    width: 100%;
    min-width: 0;
}

.kst-searchable-native {
    position: absolute !important;
    inset: auto auto 0 0;
    width: 1px !important;
    min-width: 1px !important;
    height: 1px !important;
    min-height: 1px !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
}

.kst-searchable-control {
    display: flex !important;
    grid-template-columns: none !important;
    flex-flow: row nowrap !important;
    gap: 0 !important;
    position: relative;
    width: 100%;
    min-width: 0;
}

/* Searchable controls also live in dynamically inserted modal fragments,
   outside .content. Keep the arrow inside the field on every surface. */
.kst-searchable-select { display: block !important; min-width: 0; width: 100%; }
.kst-searchable-select .kst-searchable-control > .kst-searchable-input {
    flex: 1 1 0;
    width: 100% !important;
    min-width: 0;
    padding-right: 36px !important;
}
.kst-searchable-select .kst-searchable-control > .kst-searchable-toggle,
.kst-searchable-select .kst-searchable-control > .kst-searchable-toggle:hover {
    display: flex !important;
    position: absolute !important;
    top: 1px;
    right: 1px;
    height: calc(100% - 2px) !important;
    align-items: center;
    justify-content: center;
    flex: 0 0 36px;
    width: 36px !important;
    min-width: 36px !important;
    min-height: 0;
    align-self: stretch;
    margin: 0;
    padding: 0;
    color: var(--green-950, #005543);
    background: transparent !important;
    border: 0 !important;
    box-shadow: none !important;
    border-radius: 0;
    font-size: 0;
}
.kst-searchable-select .kst-searchable-toggle::before {
    content: "";
    width: 12px;
    height: 8px;
    background: var(--ui-select-chevron) center / 12px 8px no-repeat;
}

.content .kst-searchable-input {
    width: 100%;
    min-width: 0;
    padding-right: 36px !important;
    text-overflow: ellipsis;
}

.content .kst-searchable-toggle,
.content .kst-searchable-toggle:hover,
.content .kst-searchable-toggle:focus-visible {
    position: absolute;
    top: 1px;
    right: 1px;
    width: 34px;
    min-width: 34px;
    height: calc(100% - 2px);
    min-height: 0;
    padding: 0;
    color: var(--green-900);
    background: transparent;
    border: 0;
    border-radius: 0 5px 5px 0;
    box-shadow: none;
    font-size: 18px;
    line-height: 1;
}

.content .kst-searchable-toggle:hover,
.content .kst-searchable-toggle:focus-visible {
    background: var(--green-50);
}

.kst-searchable-select.is-open .kst-searchable-input {
    border-color: var(--green-700);
    box-shadow: 0 0 0 3px rgba(11, 107, 79, .10);
}

.kst-searchable-select.is-disabled .kst-searchable-toggle {
    opacity: .45;
}

.kst-searchable-list {
    position: fixed;
    z-index: 500;
    min-width: 180px;
    padding: 4px;
    overflow-y: auto;
    overscroll-behavior: contain;
    color: var(--text);
    background: #fff;
    border: 1px solid var(--line-strong);
    border-radius: 7px;
    box-shadow: 0 12px 30px rgba(0, 60, 44, .16);
}

.kst-searchable-option {
    display: block;
    width: 100%;
    min-height: 34px;
    padding: 7px 9px;
    color: var(--text);
    background: transparent;
    border: 0;
    border-radius: 5px;
    font: inherit;
    line-height: 1.35;
    text-align: left;
    white-space: normal;
    cursor: pointer;
}

.kst-searchable-option:hover,
.kst-searchable-option.is-active {
    color: var(--green-950);
    background: var(--green-100);
}

.kst-searchable-option[aria-selected="true"] {
    font-weight: 800;
}

.kst-searchable-list--person .kst-searchable-option {
    display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
    padding: 7px 10px; min-height: 36px; height: auto;
    font-size: 14px !important; font-weight: 600 !important; line-height: 1.35;
    white-space: nowrap;
}
.kst-person-name, .kst-person-number { display: block; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kst-person-name { font-size: 14px; font-weight: 600; }
.kst-person-number { font-size: 12px; font-weight: 400; color: var(--muted); }

.kst-searchable-empty,
.kst-searchable-limit {
    padding: 9px;
    color: var(--muted);
    line-height: 1.4;
}

.kst-searchable-limit {
    border-top: 1px solid var(--line);
    font-size: 12px;
}

@media (max-width: 720px) {
    .kst-searchable-list {
        max-width: calc(100vw - 16px);
    }

    .kst-searchable-option {
        min-height: 40px;
        padding: 9px 10px;
    }
}

:where(.content input[type="checkbox"]),
:where(.content input[type="radio"]) {
    accent-color: var(--green-800);
}

:where(
    .content button:not(.modal-close):not(.approval-modal-close):not(.message-dismiss)
) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--form-control-height);
    padding: 0 13px;
    color: #fff;
    background: var(--green-900);
    border: 1px solid var(--green-900);
    border-radius: 6px;
    font: inherit;
    font-size: 13px;
    font-weight: 800;
    line-height: 1.2;
    white-space: nowrap;
    cursor: pointer;
    transition: color .16s ease, background .16s ease, border-color .16s ease;
}

:where(
    .content button:not(.modal-close):not(.approval-modal-close):not(.message-dismiss):hover
),
:where(
    .content button:not(.modal-close):not(.approval-modal-close):not(.message-dismiss):focus-visible
) {
    background: var(--green-800);
    border-color: var(--green-800);
}

:where(.content button:focus-visible) {
    outline: 3px solid rgba(11, 107, 79, .16);
    outline-offset: 2px;
}

:where(.content button:disabled) {
    opacity: .55;
    cursor: not-allowed;
}

:where(
    .crm-form,
    .erp-form,
    .member-edit-form,
    .profile-form,
    .approval-form,
    .approval-modal-form,
    .user-form-grid,
    .permission-role-selector,
    .permission-default-approver-form
) label {
    gap: var(--form-field-gap);
}

:where(
    .form-grid,
    .erp-form-grid,
    .org-select-grid,
    .user-form-grid,
    .profile-form-grid,
    .profile-password-grid
) {
    row-gap: var(--form-grid-row-gap);
    column-gap: var(--form-grid-column-gap);
}

.crm-form textarea,
.erp-form textarea,
.member-edit-form textarea,
.profile-form textarea {
    min-height: 56px;
    padding: 7px 10px;
}

.form-section {
    gap: var(--form-section-gap);
    padding-bottom: var(--form-section-padding);
}

.form-section + .form-section {
    padding-top: var(--form-section-padding);
}

/* Full-page quotation editor and read-only quotation detail. */
.quotation-create-form .form-section {
    gap: 8px;
    padding: 12px 16px;
}

.quotation-create-form .form-section + .form-section {
    padding-top: 12px;
}

.quotation-create-form .form-grid {
    gap: 8px 10px;
}

.quotation-create-form .form-grid > label,
.quotation-create-form .quotation-detail-row > div {
    gap: 4px;
    line-height: 1.3;
}

.quotation-create-form input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
.quotation-create-form select {
    min-height: 36px;
    padding: 0 10px;
}

.quotation-create-form textarea {
    height: 36px;
    min-height: 36px;
    padding: 6px 10px;
    line-height: 22px;
}

.quotation-create-form .form-section h3 {
    font-size: 16px;
}

.quotation-create-form .form-section-heading p,
.quotation-create-form .field-help {
    line-height: 1.4;
}

.quotation-create-form .review-department-options > div {
    gap: 8px;
}

.quotation-create-form .review-department-option,
.quotation-create-form .review-department-options label {
    min-height: 38px;
    padding: 7px 10px;
}

.review-department-approver {
    padding: 6px 8px;
}

.quotation-create-actions {
    gap: 8px;
    padding: 10px 16px;
}

/* Table-like product detail rows intentionally remain denser. */
.quotation-create-form .quotation-detail-row > div > input,
.quotation-create-form .quotation-detail-row > div > select,
.quotation-create-form .quotation-detail-row > div > textarea {
    height: 32px;
    min-height: 32px;
    padding: 4px 7px;
}

/* Django formset DELETE checkboxes are controlled by the visible delete
 * buttons.  They must remain in the form for submission without inheriting
 * the full-width CRM input contract and widening the entire page. */
.quotation-create-form .quotation-detail-head > span:last-child,
.quotation-create-form .quotation-detail-row .detail-delete {
    position: sticky;
    right: 0;
    z-index: 2;
    background: inherit;
    box-shadow: -8px 0 12px -12px rgba(0, 54, 39, 0.45);
}

.quotation-create-form .quotation-detail-head > span:last-child {
    z-index: 3;
}

.quotation-create-form .quotation-detail-row .detail-delete input[type="checkbox"] {
    position: absolute;
    top: 0;
    left: 0;
    width: 1px;
    min-width: 1px;
    height: 1px;
    min-height: 1px;
    margin: 0;
    padding: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
}

/* ERP forms use the same rhythm without changing their column definitions. */
.erp-product-form,
.erp-product-form-section {
    gap: 8px;
}

.erp-product-form-section + .erp-product-form-section,
.erp-form-section + .erp-form-section {
    margin-top: 0;
    padding-top: 10px;
}

.erp-form-section h3 {
    margin-bottom: 6px;
}

.erp-formula-form {
    gap: 8px;
}

.erp-formula-form .erp-field-wide {
    margin-top: 4px;
}

.erp-product-image-row {
    gap: 10px;
    padding: 8px 10px;
}

.erp-form-submit,
.erp-shipping-items {
    margin-top: 10px;
}

/* System management and profile forms. */
.member-edit-form,
.user-editor,
.permission-management-page {
    gap: 10px;
}

.user-form-grid {
    padding: 12px 14px;
}

.user-form-grid textarea {
    min-height: 58px;
    padding: 7px 10px;
}

.profile-section-header {
    padding: 12px 14px 0;
}

.profile-form-grid,
.profile-password-grid {
    padding: 10px 14px;
}

.profile-form-actions {
    padding: 0 14px 14px;
}

.permission-role-selector {
    padding: 12px 14px;
}

/* Modal shell: more useful content above the fold, same touch targets. */
.modal-header,
.approval-modal-header,
.system-modal-panel > header {
    gap: 12px;
    padding: 15px 18px;
}

.modal-header p,
.approval-modal-header p {
    margin-top: 3px;
}

.modal-form,
.modal-body,
.approval-modal-body,
.system-modal-body {
    padding: 12px 14px 14px;
}

.modal-actions,
.approval-modal-actions,
.crm-form .modal-actions {
    gap: 8px;
    padding-top: 10px;
}

.modal-actions button,
.approval-modal-actions button,
.crm-form .modal-actions button {
    min-height: 36px;
    padding: 0 15px;
}

.approval-detail-grid {
    gap: 8px;
    margin-bottom: 10px;
}

.approval-detail-grid div,
.approval-reason {
    padding: 9px 10px;
}

.approval-reason {
    margin-bottom: 10px;
}

.approval-modal-form label {
    gap: 4px;
}

.approval-modal-form textarea {
    min-height: 72px;
}

@media (max-width: 720px) {
    .quotation-create-form .form-section {
        padding: 12px;
    }

    .quotation-create-actions {
        padding: 10px 12px;
    }

    .modal-header,
    .approval-modal-header,
    .system-modal-panel > header,
    .modal-form,
    .modal-body,
    .approval-modal-body,
    .system-modal-body {
        padding-left: 12px;
        padding-right: 12px;
    }
}
