/* =================================================================
   components/app-journal.css — .app-journal component
   -----------------------------------------------------------------
   An editable, compact accounting-row grid for journal entries,
   supplier/customer invoice postings and voucher rows. Every body
   cell is an inline input; the table is a form, not a data display.

   BEM naming: block .app-journal, elements __head, __body, __row,
   __th, __td, __input, __foot, __picker, modifiers for alignment,
   readonly state and summary labels.

   Designed to replace the legacy .novus_edit_table while keeping
   the same compact density and zero-chrome input feel.
   ================================================================= */

/* ========== 1. BASE ========== */

.app-journal {
    /* Scope semantic tokens to neutral for the journal */
    --bg-tertiary: var(--neutral-50);
    --bg-secondary: var(--neutral-100);
    --border-secondary: var(--neutral-200);
    --border-tertiary: var(--neutral-150);
    --text-secondary: var(--neutral-600);
    --text-tertiary: var(--neutral-500);

    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: auto;
    font-family: var(--font-family-primary);
    font-size: var(--font-body-sm-size);
    line-height: var(--font-body-sm-line-height);
    color: var(--text-primary);
    background: var(--bg-primary);
    border: 1px solid var(--border-secondary);
    border-radius: var(--radius-lg);
    overflow: clip;
}

/* ========== 2. HEAD ========== */

.app-journal__head {
    background: var(--bg-tertiary);
}

.app-journal__th {
    padding: var(--spacing-xs) var(--spacing-sm);
    font-size: var(--font-caption-sm-size);
    font-weight: var(--font-weight-semibold);
    color: var(--text-secondary);
    text-align: left;
    text-transform: uppercase;
    letter-spacing: var(--font-caption-sm-letter-spacing);
    line-height: var(--font-caption-sm-line-height);
    border-bottom: 1px solid var(--border-secondary);
    white-space: normal;
    user-select: none;
    vertical-align: middle;
    /* Same header height as app-table, whatever icons a page's columns carry. */
    height: 33px;
}

/* ========== 3. BODY ========== */

.app-journal__body .app-journal__row {
    transition: background-color var(--transition-fast);
    font-weight: var(--font-weight-medium);
}

.app-journal__body .app-journal__row:hover {
    background-color: var(--color-hover-bg);
}

/* ========== 4. CELLS ========== */

.app-journal__td {
    padding: 0;
    border-bottom: 1px solid var(--border-secondary);
    border-right: 1px solid var(--border-tertiary);
    vertical-align: middle;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-journal__td:last-child {
    border-right: none;
}

.app-journal__body .app-journal__row:last-child .app-journal__td {
    border-bottom: none;
}

/* ========== 5. INLINE INPUT ========== */

.app-journal__input {
    display: block;
    width: 100%;
    height: 32px;
    padding: 0 var(--spacing-sm);
    margin: 0;
    border: none;
    outline: none;
    background: transparent;
    font-family: inherit;
    font-size: inherit;
    font-weight: inherit;
    line-height: 32px;
    color: inherit;
    box-sizing: border-box;
}

.app-journal__input:focus {
    background: var(--bg-primary);
    box-shadow: inset 0 0 0 1.5px var(--color-focus-ring);
    border-radius: var(--radius-sm);
    position: relative;
    z-index: 1;
}

/* Autocomplete loading — suppress jQuery UI's background-image gif */
.app-journal__input.ui-autocomplete-loading {
    background-image: none !important;
}

/* Inline actions — input + action icon(s) side by side in one cell */
.app-journal__td--has-actions {
    display: flex;
    align-items: center;
    overflow: visible;
}

.app-journal__td--has-actions .app-journal__input {
    flex: 1;
    min-width: 0;
}

.app-journal__td--has-actions .app-action {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border-radius: var(--radius-sm);
    color: var(--accent);
    cursor: pointer;
    transition: color var(--transition-fast), background-color var(--transition-fast);
}

.app-journal__td--has-actions .app-action:hover {
    color: var(--text-primary);
    background-color: var(--color-hover-bg);
}

/* Readonly inputs get a subtle tinted background.
   Exclude <select>: browsers always match :read-only on selects (readonly is not a valid attribute). */
.app-journal__input:read-only:not(select) {
    background: var(--bg-secondary);
    color: var(--text-secondary);
}

/* On row hover, push editable inputs slightly so user sees affordance */
.app-journal__body .app-journal__row:hover .app-journal__input:not(:read-only):not(:disabled):not(:focus) {
    background: color-mix(in srgb, var(--color-focus-ring) 6%, transparent);
}

/* Row hover must also tint readonly inputs to match the row bg */
.app-journal__body .app-journal__row:hover .app-journal__input:read-only:not(select) {
    background: color-mix(in srgb, var(--bg-tertiary) 50%, transparent);
}

/* ========== 6. COLUMN SIZING ========== */

/* Every column hugs its widest cell; the table scrolls if it overflows. */
.app-journal__td {
    width: 1%;
    white-space: nowrap;
    min-width: calc(var(--chars, 4) * 1ch + var(--spacing-md) * 2);
}

/* A flex cell is not a table cell, so width: 1% shrinks it and leaves a gap in its column. */
.app-journal__td--has-actions {
    width: auto;
}

/* The body sizes the columns; footer filler cells must not widen them. */
.app-journal__foot .app-journal__td {
    min-width: 0;
}

/* Trailing footer fillers read as one blank area, not a row of empty boxes. */
.app-journal__foot .app-journal__td--filler {
    border-right: none;
    font-size: 0;
}

.app-journal__th--fill { width: 100%; }
/* Two fill columns share the space instead of the first taking it all. */
.app-journal__th--fill:has(~ .app-journal__th--fill),
.app-journal__th--fill ~ .app-journal__th--fill { width: 50%; }
.app-journal__th--sm   { min-width: 80px;  }
.app-journal__th--md   { min-width: 120px; }
.app-journal__th--lg   { min-width: 160px; }

/* Percentage widths — mirror legacy invoice/voucher table proportions */
.app-journal__th--w2   { width: 2%;  }
.app-journal__th--w3   { width: 3%;  }
.app-journal__th--w4   { width: 4%;  }
.app-journal__th--w5   { width: 5%;  }
.app-journal__th--w6   { width: 6%;  }
.app-journal__th--w7   { width: 7%;  }
.app-journal__th--w8   { width: 8%;  }

/* ========== 7. ALIGNMENT ========== */

.app-journal__th--right,
.app-journal__td--right {
    text-align: right;
}

.app-journal__td--right .app-journal__input {
    text-align: right;
}

.app-journal__th--center,
.app-journal__td--center {
    text-align: center;
}

.app-journal__td--center .app-journal__input {
    text-align: center;
}

/* ========== 7. ACTION CELLS ========== */

/* An empty action cell collapses but stays in the row: display: none would shift the later cells a column left.
   Doubled class so it beats the action-cell padding below; font-size drops the &nbsp; placeholder's width. */
.app-journal__td.app-journal__td--actions:not(:has(*)),
.app-journal__th.app-journal__th--actions:not(:has(*)) {
    padding: 0;
    font-size: 0;
}

.app-journal__td--actions {
    width: 1px;
    /* Icon cells stay icon-wide; the column-hugging min-width above is for text cells. */
    min-width: 0;
    white-space: nowrap;
    padding: 0 var(--spacing-xs);
    height: 32px;
    text-align: center;
    overflow: visible;
    border-right: none;
}

/* Action header cells hug content too */
.app-journal__th--actions {
    width: 1px;
    white-space: nowrap;
}

.app-journal__td--actions .app-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border-radius: var(--radius-sm);
    color: var(--text-tertiary);
    cursor: pointer;
    transition: color var(--transition-fast), background-color var(--transition-fast);
    vertical-align: middle;
}

.app-journal__td--actions .app-action:hover {
    color: var(--text-primary);
    background-color: var(--color-hover-bg);
}

.app-journal__td--actions .app-action.is-disabled {
    color: var(--color-disabled-text);
    cursor: default;
    pointer-events: none;
}

/* Legacy icon bridge — the existing markup wraps .icon SVGs in a <span>.
   Style the span as the click target so the hit area is larger than the SVG. */
.app-journal__td--actions > span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: color var(--transition-fast), background-color var(--transition-fast);
    color: var(--text-tertiary);
}

.app-journal__td--actions > span:hover {
    color: var(--text-primary);
    background-color: var(--color-hover-bg);
}

.app-journal__td--actions .icon {
    width: 16px;
    height: 16px;
    pointer-events: none;
}

/* ========== 8. HEADER PICKER ICON ========== */

.app-journal__picker {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: var(--spacing-xs);
    width: 16px;
    height: 16px;
    color: var(--text-tertiary);
    cursor: pointer;
    /* 16px box on a 12px header line: the negative margins stop it growing the line and lifting the label. */
    vertical-align: top;
    margin-block: -2px;
    transition: color var(--transition-fast);
}

/* Keep a header's label and its picker on one line — the column grows instead. */
.app-journal__th:has(> .app-journal__picker) {
    white-space: nowrap;
}

.app-journal__picker:hover {
    color: var(--text-primary);
}

.app-journal__picker .icon {
    width: var(--icon-size-md);
    height: var(--icon-size-md);
}

/* ========== 9. FOOT (TOTALS) ========== */

.app-journal__foot {
    background: var(--bg-tertiary);
}

.app-journal__foot .app-journal__td {
    padding: 0;
    border-bottom: none;
    border-top: 1px solid var(--border-secondary);
}

.app-journal__foot .app-journal__row:last-child .app-journal__td {
    border-top: none;
}

.app-journal__foot .app-journal__row:first-child .app-journal__td {
    border-top: 1px solid var(--border-secondary);
}

/* Summary label (e.g. "Sum", "Difference") */
.app-journal__label {
    padding: 0 var(--spacing-sm);
    font-size: var(--font-caption-sm-size);
    font-weight: var(--font-weight-semibold);
    color: var(--text-secondary);
    text-align: right;
    line-height: 32px;
}

/* The footer's padding: 0 above would otherwise push the label onto the divider. */
.app-journal__foot .app-journal__label {
    padding: 0 var(--spacing-sm);
    line-height: 24px;
}

/* Footer inputs inherit the compact height */
.app-journal__foot .app-journal__input {
    background: transparent;
    font-weight: var(--font-weight-semibold);
    /* Totals rows are compact: two full-height rows read as heavy under a short journal. */
    height: 24px;
    line-height: 24px;
}

.app-journal__foot .app-journal__input:read-only {
    background: transparent;
}

/* ========== 10. SELECT CELLS ========== */

/* A select's text takes the row's size, not the 14px form-field size Briox.Select gives it elsewhere. */
.app-journal .novus_filter_select .selected_filter_text {
    font-size: inherit;
}

/* Selects in journal rows need visible overflow for the dropdown */
.app-journal__td--select {
    overflow: visible;
}

.app-journal__td--select:focus-within {
    background: var(--bg-tertiary);
}

/* A Briox.Select in a row: anchor its list to the cell, not to <main>. */
.app-journal__td .novus_filter_select {
    position: relative;
}

/* ========== 11. ERROR STATE ========== */

.app-journal__input.is-error {
    background: color-mix(in srgb, var(--color-error) 8%, transparent);
}

.app-journal__input.is-error:focus {
    box-shadow: inset 0 0 0 1.5px var(--color-error);
}

/* ========== 12. DISABLED ROW ========== */

.app-journal__row.is-disabled .app-journal__td,
.app-journal__row[disabled] .app-journal__td {
    opacity: 0.8;
    pointer-events: none;
}

.app-journal__row.is-disabled .app-journal__td--actions,
.app-journal__row[disabled] .app-journal__td--actions {
    pointer-events: auto;
    opacity: 1;
}

/* A locked record: every cell takes the read-only tint, not just the ones an input fills. */
.app-journal--locked .app-journal__body .app-journal__td {
    background: var(--bg-secondary);
}

.app-journal--locked .app-journal__body .app-journal__row:hover .app-journal__td {
    background: var(--bg-tertiary);
}

.app-journal--locked .novus_filter_select {
    background: transparent;
}

/* Row tools are off on a locked record: no drag, no insert or delete, and no tooltips from the cell. */
.app-journal--locked .app-journal__td--actions {
    pointer-events: none;
}

.app-journal--locked .app-journal__td--actions .app-action {
    color: var(--color-disabled-text);
}

/* ========== 13. RESPONSIVE ==========
   The journal's column min-widths create a ~840px floor that prevents the page from
   shrinking further. The wrapper scrolls horizontally instead. A container query on
   the wrapper hides low-priority columns (account name, balance) to keep the core
   editable columns visible longer before scrolling kicks in. */

.app-journal__wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    min-width: 0;
    container-type: inline-size;
    /* Scroll shadows — fade in/out at each edge via background-attachment: local vs scroll */
    background:
        linear-gradient(to right, var(--bg-primary) 30%, transparent) left center / 40px 100% no-repeat local,
        linear-gradient(to left,  var(--bg-primary) 30%, transparent) right center / 40px 100% no-repeat local,
        linear-gradient(to right, rgba(0,0,0,0.08), transparent) left center / 12px 100% no-repeat scroll,
        linear-gradient(to left,  rgba(0,0,0,0.08), transparent) right center / 12px 100% no-repeat scroll;
    /* auto, not scroll: a persistent track drew an empty grey band under a table that fits. */
    overflow-x: auto;
}

/* Thin persistent scrollbar — WebKit/Blink */
.app-journal__wrapper::-webkit-scrollbar {
    height: 6px;
}

.app-journal__wrapper::-webkit-scrollbar-track {
    background: var(--bg-secondary);
    border-radius: 3px;
}

.app-journal__wrapper::-webkit-scrollbar-thumb {
    background: var(--neutral-400, #adb5bd);
    border-radius: 3px;
}

.app-journal__wrapper::-webkit-scrollbar-thumb:hover {
    background: var(--neutral-500, #868e96);
}

/* Firefox — thin persistent scrollbar */
.app-journal__wrapper {
    scrollbar-width: thin;
    scrollbar-color: var(--neutral-400, #adb5bd) var(--bg-secondary);
}

.app-journal__scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Give the table a min-width floor so columns don't collapse below usability */
.app-journal {
    min-width: 600px;
}

/* Shrink header/input padding at narrow widths to reclaim space */
@container (max-width: 600px) {
    .app-journal__th {
        padding: var(--spacing-xs) 4px;
        font-size: 8px;
    }

    .app-journal__input {
        height: 28px;
        padding: 0 4px;
        font-size: var(--font-caption-sm-size);
        line-height: 28px;
    }

    .app-journal__th--sm {
        min-width: 60px;
    }

    .app-journal__label {
        padding: 0 4px;
        font-size: 10px;
        line-height: 24px;
    }

    .app-journal__foot .app-journal__input {
        height: 24px;
        line-height: 24px;
    }
}

/* ========== 14. REDUCED MOTION ========== */

@media (prefers-reduced-motion: reduce) {
    .app-journal__body .app-journal__row {
        transition: none;
    }
}