/*
   CraftGrid Finance & Accounting - mobile polish
   Presentation only. No accounting, posting, reporting, or workflow logic changes.
   Desktop layouts remain unchanged; mobile screens switch dense accounting tables to
   readable card-like rows so users never need horizontal scrolling for core data.
*/

.cg-accounting-page { min-width: 0; }

@media (max-width: 767.98px) {
    .cg-accounting-page,
    .cg-accounting-page .card,
    .cg-accounting-page .card-body,
    .gl-page,
    .tb-page,
    .bs-page,
    .pnl-page { min-width: 0; }

    /* Page headings / actions */
    .cg-accounting-page > .d-flex:first-child,
    .cg-accounting-reconciliation > .d-flex:first-child,
    .cg-accounting-posting-profiles > .d-flex:first-child,
    .cg-accounting-cost-centers > .d-flex:first-child {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: .7rem !important;
    }

    .cg-accounting-page > .d-flex:first-child > div:first-child,
    .cg-accounting-reconciliation > .d-flex:first-child > div:first-child {
        min-width: 0;
    }

    .cg-accounting-page h2,
    .cg-accounting-reconciliation h2 {
        font-size: 1.45rem;
        line-height: 1.15;
        overflow-wrap: anywhere;
    }

    .cg-accounting-page > .d-flex:first-child > .btn,
    .cg-accounting-posting-profiles > .d-flex:first-child > .btn,
    .cg-accounting-cost-centers > .d-flex:first-child > .btn {
        width: 100%;
        min-height: 44px;
    }

    /* Filter cards: one comfortable field per row. */
    .cg-accounting-page form .row > [class*="col-md-"],
    .cg-accounting-page form .row > [class*="col-lg-"],
    .cg-accounting-reconciliation form .row > [class*="col-md-"] {
        width: 100%;
        flex: 0 0 100%;
    }

    .cg-accounting-page form .row > [class*="col-md-"] .btn,
    .cg-accounting-page form .row > [class*="col-lg-"] .btn,
    .cg-accounting-reconciliation form .row > [class*="col-md-"] .btn {
        min-height: 44px;
    }

    .cg-accounting-page form .row > .d-flex,
    .cg-accounting-reconciliation form .row > .d-flex {
        display: grid !important;
        grid-template-columns: 1fr 1fr;
        gap: .55rem !important;
    }

    /* Generic accounting table -> mobile cards. */
    .cg-accounting-mobile-table {
        width: 100% !important;
        min-width: 0 !important;
        display: block;
        border-collapse: separate !important;
        border-spacing: 0;
    }

    .cg-accounting-mobile-table thead { display: none; }

    .cg-accounting-mobile-table tbody,
    .cg-accounting-mobile-table tbody tr {
        display: block;
        width: 100%;
    }

    .cg-accounting-mobile-table tbody tr {
        padding: .7rem .8rem;
        border-bottom: 1px solid #e3e8ee;
        background: #fff;
    }

    .cg-accounting-mobile-table tbody tr:last-child { border-bottom: 0; }

    .cg-accounting-mobile-table tbody td {
        display: grid;
        grid-template-columns: minmax(92px, 36%) minmax(0, 1fr);
        gap: .55rem;
        align-items: start;
        width: 100%;
        min-width: 0;
        padding: .4rem 0;
        border: 0 !important;
        text-align: left !important;
        white-space: normal !important;
        overflow-wrap: anywhere;
    }

    .cg-accounting-mobile-table tbody td::before {
        content: attr(data-label);
        display: block;
        color: #64788b;
        font-size: .68rem;
        font-weight: 750;
        letter-spacing: .035em;
        line-height: 1.25;
        text-transform: uppercase;
    }

    .cg-accounting-mobile-table tbody td[data-label=""]::before,
    .cg-accounting-mobile-table tbody td[colspan]::before { content: none; }

    .cg-accounting-mobile-table tbody td > * { min-width: 0; max-width: 100%; }

    .cg-accounting-mobile-table tbody td[data-label="Account"],
    .cg-accounting-mobile-table tbody td[data-label="Journal"],
    .cg-accounting-mobile-table tbody td[data-label="Code"] {
        font-weight: 600;
    }

    .cg-accounting-mobile-table tbody td[data-label="Actions"],
    .cg-accounting-mobile-table tbody td[data-label="Action"] {
        grid-template-columns: 1fr;
        margin-top: .35rem;
        padding-top: .65rem;
        border-top: 1px solid #e6ebef !important;
    }

    .cg-accounting-mobile-table tbody td[data-label="Actions"]::before,
    .cg-accounting-mobile-table tbody td[data-label="Action"]::before {
        margin-bottom: .35rem;
    }

    .cg-accounting-mobile-table tbody td[data-label="Actions"] .btn,
    .cg-accounting-mobile-table tbody td[data-label="Action"] .btn {
        min-height: 40px;
    }

    .cg-accounting-mobile-table tbody td[data-label="Actions"] > .btn + .btn {
        margin-left: .35rem !important;
    }

    /* Financial amounts stay visually aligned to the value side of the card. */
    .cg-accounting-mobile-table tbody td.text-end[data-label] > * {
        text-align: right;
    }

    /* Chart of Accounts: keep hierarchy compact but readable. */
    .cg-accounting-coa .craftgrid-coa-account {
        min-width: 0;
        width: 100%;
        padding-left: calc(var(--coa-depth) * .7rem);
        align-items: flex-start;
        overflow-wrap: anywhere;
    }

    .cg-accounting-coa .craftgrid-coa-table tbody td[data-label="Account"] > .craftgrid-coa-account {
        grid-column: 1 / -1;
    }

    /* Reconciliation: exactly the layout shown in the requested mobile design. */
    .cg-accounting-reconciliation .table-responsive {
        overflow: visible !important;
    }

    .cg-accounting-reconciliation .cg-accounting-mobile-table tbody tr {
        border: 1px solid #e3e8ee;
        border-radius: 12px;
        margin-bottom: .65rem;
        box-shadow: 0 2px 8px rgba(15,23,42,.035);
    }

    .cg-accounting-reconciliation .cg-accounting-mobile-table tbody tr:last-child { margin-bottom: 0; }

    /* GL / Trial Balance: remove their table wrappers' horizontal scroll on mobile. */
    .gl-table-wrap,
    .tb-table-wrap { overflow: visible !important; }

    .gl-table tbody tr,
    .tb-table tbody tr { box-shadow: 0 2px 8px rgba(15,23,42,.03); }

    .gl-table tbody td[data-label="Account"] .gl-account-link,
    .tb-table tbody td[data-label="Account"] a { overflow-wrap: anywhere; }

    .gl-table tbody td[data-label="Action"] a {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 42px;
        min-height: 40px;
    }

    /* Journal page already has its own mature mobile treatment; only constrain width. */
    .journal-page,
    .journal-grid-card,
    .journal-filter-card { min-width: 0; }

    /* Report hero/filter blocks */
    .gl-hero,
    .tb-hero,
    .bs-hero,
    .pnl-card,
    .gl-card,
    .tb-card { max-width: 100%; }

    .gl-hero,
    .tb-hero,
    .bs-hero { padding: 16px !important; }

    .gl-card-body,
    .tb-card-body { padding: 14px !important; }

    .gl-hero .row > [class*="col-"],
    .tb-hero .row > [class*="col-"],
    .bs-hero .row > [class*="col-"],
    .pnl-hero .row > [class*="col-"] {
        width: 100%;
        flex: 0 0 100%;
    }

    .gl-hero .btn,
    .tb-hero .btn,
    .bs-hero .btn,
    .pnl-hero .btn { width: 100%; min-height: 44px; }

    /* Financial statement summary rows wrap instead of squeezing. */
    .bs-line,
    .pnl-line,
    .bs-section-header,
    .pnl-section-header {
        gap: .65rem;
        flex-wrap: wrap;
    }

    .bs-line > span:first-child,
    .pnl-line > span:first-child { min-width: 0; overflow-wrap: anywhere; }

    /* Pagination should never overflow the viewport. */
    .cg-accounting-page .pagination,
    .cg-accounting-page nav .pagination,
    .cg-accounting-reconciliation .pagination {
        flex-wrap: wrap;
        gap: .25rem;
    }
}

@media (max-width: 379.98px) {
    .cg-accounting-mobile-table tbody td {
        grid-template-columns: minmax(78px, 40%) minmax(0, 1fr);
        gap: .45rem;
        font-size: .84rem;
    }

    .cg-accounting-mobile-table tbody td::before { font-size: .64rem; }

    .cg-accounting-coa .craftgrid-coa-account { padding-left: calc(var(--coa-depth) * .5rem); }
}

/* -------------------------------------------------------------
   Accounting contextual help
   A small lightbulb is deliberately placed beside the section
   heading. It opens a plain-language explanation without taking
   permanent space on the page.
   ------------------------------------------------------------- */
.cg-accounting-help-wrap {
    position: relative;
    display: inline-flex;
    vertical-align: middle;
    margin: 0 0 0 .45rem;
}

.cg-accounting-help-button {
    width: 32px;
    height: 32px;
    padding: 0;
    border: 1px solid #d9e3ea;
    border-radius: 50%;
    background: #fff8df;
    color: #b17b00;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: .15s ease;
}

.cg-accounting-help-button:hover,
.cg-accounting-help-button:focus-visible,
.cg-accounting-help-button[aria-expanded="true"] {
    background: #fff1b8;
    border-color: #d8b15a;
    color: #8a6100;
    outline: none;
    box-shadow: 0 0 0 3px rgba(216,177,90,.18);
}

.cg-accounting-help-popover {
    position: absolute;
    z-index: 1080;
    top: calc(100% + 9px);
    left: 0;
    width: min(360px, calc(100vw - 40px));
    padding: 13px 14px;
    border: 1px solid #dbe4ea;
    border-radius: 12px;
    background: #fff;
    box-shadow: 0 10px 28px rgba(15,23,42,.14);
    color: #30465b;
    font-size: .86rem;
    line-height: 1.5;
}

.cg-accounting-help-title {
    display: flex;
    align-items: center;
    gap: 7px;
    margin-bottom: 6px;
    color: #173f35;
    font-weight: 750;
}

.cg-accounting-help-title .bi {
    color: #b17b00;
}

.cg-accounting-help-text {
    color: #536b7f;
}

/* The Account field is a stacked identity block on narrow screens.
   This prevents the account code from being squeezed into the label
   column while the account name wraps underneath it. */
@media (max-width: 767.98px) {
    .cg-accounting-reconciliation .cg-accounting-mobile-table tbody td[data-label="Account"],
    .cg-accounting-coa .cg-accounting-mobile-table tbody td[data-label="Account"] {
        display: block;
        padding-top: .35rem;
    }

    .cg-accounting-reconciliation .cg-accounting-mobile-table tbody td[data-label="Account"]::before,
    .cg-accounting-coa .cg-accounting-mobile-table tbody td[data-label="Account"]::before {
        margin-bottom: .25rem;
    }

    .cg-accounting-reconciliation .cg-accounting-mobile-table tbody td[data-label="Account"] strong,
    .cg-accounting-coa .cg-accounting-mobile-table tbody td[data-label="Account"] strong {
        display: inline-block;
        margin-right: .15rem;
        font-size: .98rem;
    }

    .cg-accounting-help-popover {
        position: fixed;
        top: auto;
        left: 20px;
        right: 20px;
        bottom: 18px;
        width: auto;
        max-width: none;
    }
}

/* -------------------------------------------------------------
   Accounting column help
   Small info icons explain accounting table columns without adding
   permanent text or changing any accounting/reporting logic.
   ------------------------------------------------------------- */
.cg-accounting-column-help-table th {
    position: relative;
}

.cg-accounting-column-help {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: .3rem;
    vertical-align: middle;
}

.cg-accounting-column-help-button {
    width: 19px;
    height: 19px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: #6b8193;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: help;
    font-size: .72rem;
    line-height: 1;
}

.cg-accounting-column-help-button:hover,
.cg-accounting-column-help-button:focus-visible {
    color: #174d43;
    background: #eef6f3;
    outline: none;
    box-shadow: 0 0 0 2px rgba(23,77,67,.12);
}

.cg-accounting-column-help-tooltip {
    position: absolute;
    z-index: 1090;
    left: 50%;
    top: calc(100% + 8px);
    transform: translateX(-50%);
    width: min(290px, 70vw);
    padding: 9px 11px;
    border: 1px solid #d8e2e8;
    border-radius: 9px;
    background: #fff;
    color: #445b6e;
    box-shadow: 0 8px 22px rgba(15,23,42,.14);
    font-size: .78rem;
    font-weight: 500;
    line-height: 1.4;
    text-align: left;
    white-space: normal;
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
    transition: opacity .12s ease, visibility .12s ease;
}

.cg-accounting-column-help-tooltip::before {
    content: "";
    position: absolute;
    top: -5px;
    left: 50%;
    width: 9px;
    height: 9px;
    background: #fff;
    border-left: 1px solid #d8e2e8;
    border-top: 1px solid #d8e2e8;
    transform: translateX(-50%) rotate(45deg);
}

.cg-accounting-column-help:hover .cg-accounting-column-help-tooltip,
.cg-accounting-column-help:focus-within .cg-accounting-column-help-tooltip {
    visibility: visible;
    opacity: 1;
}

.cg-accounting-column-help-tooltip strong {
    display: block;
    margin-bottom: 2px;
    color: #173f35;
    font-weight: 750;
}

.cg-reconciliation-summary {
    border-left-width: 4px;
}

.cg-reconciliation-summary-icon {
    margin-top: .1rem;
    font-size: 1.05rem;
}

@media (max-width: 767.98px) {
    .cg-accounting-column-help-tooltip {
        position: fixed;
        left: 20px;
        right: 20px;
        top: auto;
        bottom: 18px;
        transform: none;
        width: auto;
        max-width: none;
        z-index: 1100;
    }

    .cg-accounting-column-help-tooltip::before {
        display: none;
    }
}
