/* ============================================================================
   Global responsive + resizable layer. Loaded LAST in App.razor.

   1. Dialogs (Telerik <TelerikWindow> and the .ob-modal review dialogs)
        - never wider/taller than the viewport, content scrolls inside
        - resizable on desktop (js/window-resize.js, 2.24.0 has no Resizable parameter)
        - on phones they become a near-full-screen sheet
   2. Shared Telerik controls that overflow on small screens (pager, toolbar,
      tabstrip, popups, form inputs)
   3. Journal Voucher dialog: table turns into stacked cards when the DIALOG is
      narrow (container query, so it also reacts to the user resizing the window)
   ============================================================================ */

/* ----------------------------------------------------------------------------
   1. DIALOGS
   ---------------------------------------------------------------------------- */
.k-window {
    max-width: calc(100vw - 16px);
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px);
    min-width: min(320px, calc(100vw - 16px));
    min-height: 140px;
}

/* Let the content area shrink so the window's max-height produces an inner scrollbar. */
.k-window > .k-window-content {
    min-height: 0;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.k-window-title { min-width: 0; white-space: nowrap; }

/* Resize grip (visual only; hit-testing lives in window-resize.js). */
.k-window:not(.k-window-maximized)::after {
    content: "";
    position: absolute;
    right: 3px;
    bottom: 3px;
    width: 12px;
    height: 12px;
    pointer-events: none;
    opacity: .5;
    background: linear-gradient(135deg, transparent 0 50%, currentColor 50% 58%, transparent 58% 70%, currentColor 70% 78%, transparent 78%);
}

/* Onboarding review dialogs: width follows the viewport, and an inline size (from resizing) wins. */
.ob-overlay { overflow: auto; }
.ob-modal {
    width: min(100%, 640px);
    max-width: 100%;
    min-width: min(320px, 100%);
    max-height: 88vh;
    max-height: 88dvh;
}
.ob-modal-img { width: min(100%, 560px); max-width: 100%; }
.ob-modal-head { position: sticky; top: 0; z-index: 1; background-color: var(--surface-card-bg); }
.ob-modal-head h3 { min-width: 0; }

/* Cursor + no text-selection while the pointer is on / dragging a dialog edge. */
html[data-wp-rz="ns"] * { cursor: ns-resize !important; }
html[data-wp-rz="ew"] * { cursor: ew-resize !important; }
html[data-wp-rz="nwse"] * { cursor: nwse-resize !important; }
html[data-wp-rz="nesw"] * { cursor: nesw-resize !important; }
html[data-wp-rz-active] { user-select: none; -webkit-user-select: none; }
html[data-wp-rz-active] iframe { pointer-events: none; }

/* Phones / small tablets: full-width sheet, no resizing. */
@media (max-width: 640px) {
    .k-window:not(.k-window-maximized) {
        position: fixed !important;
        top: 8px !important;
        left: 8px !important;
        right: auto !important;
        transform: none !important;
        margin: 0 !important;
        width: calc(100vw - 16px) !important;
        min-width: 0 !important;
        height: auto !important;
        max-width: calc(100vw - 16px) !important;
        max-height: calc(100vh - 16px) !important;
        max-height: calc(100dvh - 16px) !important;
    }
    .k-window:not(.k-window-maximized)::after { display: none; }
    .k-window-titlebar { padding: 10px 12px; }
    .k-window-title { font-size: 16px; }
    .k-window > .k-window-content { padding: 12px; }

    .ob-overlay { padding: 8px; }
    .ob-modal { width: 100% !important; height: auto !important; max-height: calc(100dvh - 16px); }
    .ob-modal-head, .ob-modal-foot { padding: 12px 14px; }
    .ob-modal-body { padding: 14px; }
    .ob-img-stage { height: 52vh; margin: 8px 12px; }

    /* Dialog footers / action rows: wrap, buttons grow to fill. */
    .coa-form-actions, .ob-modal-foot { flex-wrap: wrap; }
    .coa-form-actions > .ob-btn, .coa-form-actions > button,
    .ob-modal-foot > .ob-btn, .ob-modal-foot > button { flex: 1 1 130px; justify-content: center; }
    .jv-foot { flex-direction: column; align-items: stretch; }
    .jv-foot > .ob-btn { width: 100%; justify-content: center; }
}

/* ----------------------------------------------------------------------------
   2. SHARED TELERIK CONTROLS
   ---------------------------------------------------------------------------- */
.k-popup, .k-animation-container { max-width: 100vw; }
.k-window .k-textbox, .k-window .k-input, .k-window .k-dropdown, .k-window .k-dropdownlist,
.k-window .k-combobox, .k-window .k-numerictextbox, .k-window .k-datepicker,
.k-window .k-datetimepicker, .k-window .k-maskedtextbox, .k-window .k-textarea { max-width: 100%; }

.k-grid-toolbar { flex-wrap: wrap; gap: 6px; }
.k-tabstrip-items { max-width: 100%; }
.k-tabstrip-items-wrapper, .k-tabstrip-top > .k-tabstrip-items-wrapper { overflow-x: auto; }

@media (max-width: 640px) {
    .k-pager-wrap { flex-wrap: wrap; justify-content: center; gap: 6px; }
    .k-pager-info, .k-pager-sizes { display: none !important; }
    .k-pager-numbers-wrap { max-width: 100%; }

    .k-window .k-textbox, .k-window .k-input, .k-window .k-dropdown, .k-window .k-dropdownlist,
    .k-window .k-combobox, .k-window .k-numerictextbox, .k-window .k-datepicker,
    .k-window .k-datetimepicker, .k-window .k-maskedtextbox, .k-window .k-textarea { width: 100% !important; }

    /* iOS zooms the page when a focused input is under 16px. */
    .k-input, .k-textbox, .k-textarea, .ob-textarea, .tl-search input { font-size: 16px; }

    .k-loader-container-panel { max-width: 100%; }
}

/* Keep wide page content from forcing sideways scrolling of the whole page. */
.ob-card, .mp-table-wrap, .tl-tablewrap, .am-tablewrap { max-width: 100%; }
.ob-page, .nip-page { min-width: 0; }
img { max-width: 100%; }

/* ----------------------------------------------------------------------------
   3. JOURNAL VOUCHER dialog (.jv-*) — stacked cards when the dialog is narrow
   ---------------------------------------------------------------------------- */
.jv-body { container-type: inline-size; container-name: jv; }

@container jv (max-width: 640px) {
    .jv-head { flex-direction: column; align-items: stretch; gap: 10px; }
    .jv-ref { font-size: .95rem; word-break: break-all; }

    .jv-tablewrap { border: 0; overflow: visible; }
    .jv-table { min-width: 0; display: block; }
    .jv-table thead { display: none; }
    .jv-table tbody, .jv-table tfoot { display: block; }

    .jv-table tbody tr {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        gap: 4px 12px;
        margin-bottom: 10px;
        padding: 10px 12px 10px 14px;
        border: 1px solid var(--surface-border);
        border-radius: 10px;
        position: relative;
    }
    .jv-table tbody tr:hover { background: transparent; }
    .jv-table tbody td { display: block; padding: 0; border: 0; min-width: 0; }
    .jv-table tbody td::before {
        content: attr(data-label);
        display: block;
        font-size: .64rem;
        font-weight: 700;
        letter-spacing: .05em;
        text-transform: uppercase;
        color: var(--surface-text-muted);
        font-family: inherit;
    }
    .jv-table td.jv-idx { position: absolute; top: 8px; right: 10px; text-align: right; width: auto; }
    .jv-table td.jv-idx::before { display: none; }
    .jv-table td.jv-acctname { grid-column: 1 / -1; padding-right: 26px; }
    .jv-table td.jv-narr { grid-column: 1 / -1; max-width: none; white-space: normal; overflow: visible; text-overflow: clip; }
    .jv-table td.jv-num { text-align: left; }
    .jv-table td.jv-num:empty { display: none; }
    .jv-table td.jv-empty { grid-column: 1 / -1; text-align: center; }
    .jv-table td.jv-empty::before { display: none; }
    .jv-dr-row td:first-child, .jv-cr-row td:first-child { box-shadow: none; }
    .jv-dr-row { border-left: 3px solid color-mix(in srgb, #d64550 60%, transparent) !important; }
    .jv-cr-row { border-left: 3px solid color-mix(in srgb, #1a9c54 60%, transparent) !important; }

    .jv-table tfoot tr {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        gap: 4px 12px;
        padding: 10px 14px;
        border: 2px solid var(--surface-border);
        border-radius: 10px;
    }
    .jv-table tfoot td { display: block; padding: 0; border: 0; text-align: left; }
    .jv-table tfoot td.jv-total-label { grid-column: 1 / -1; text-align: left; }
    .jv-table tfoot td:empty { display: none; }
    .jv-table tfoot td.jv-total::before {
        display: block;
        font-size: .64rem;
        letter-spacing: .05em;
        text-transform: uppercase;
        color: var(--surface-text-muted);
        content: attr(data-label);
    }
}
