/* ═══════════════════════════════════════════════════════════
   Modal — extracted from TNonline (Shared/Modal)
   Centered dialog with backdrop, header (title + close), and
   body. On small screens it becomes a full-height sheet.

   GOTCHA: the full-screen opt-in class goes on the DIALOG, not the
   container — .tn-modal-dialog--full (or .tn-modal--full on an ancestor
   of .tn-modal-dialog), never .tn-modal--full alone expecting it to
   reach the dialog by itself if your markup doesn't nest that way.
   Getting this wrong yields a normal bottom sheet with no error — a
   silent wrong variant, not a broken one, so it's easy to ship
   unnoticed. If your framework threads the class through a
   DialogClass-style parameter, double-check it lands on the element
   carrying .tn-modal-dialog specifically.
   ═══════════════════════════════════════════════════════════ */

.tn-modal-container {
    position: fixed;
    inset: 0;
    z-index: var(--tn-z-modal);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    animation: tnModalFadeIn 0.15s ease;
}

@keyframes tnModalFadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.tn-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: -1;
}

.tn-modal-dialog {
    background: var(--tn-bg-card);
    border: 1px solid var(--tn-border);
    border-radius: var(--tn-radius-lg);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    max-width: 640px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    animation: tnModalSlideUp 0.2s ease;
}

@keyframes tnModalSlideUp {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ── Size variants — desktop only.
   Same modifier-on-dialog pattern as --full: add directly to the element
   carrying .tn-modal-dialog, never wrap a separate "size" class around it.
   For content that outgrows the 640px default (a data table, a row editor).
   Mobile always collapses back to full-width in the <768px block below —
   size is a desktop concept only, the sheet stays edge-to-edge regardless
   of variant. */
.tn-modal-dialog--lg { max-width: 800px; }
.tn-modal-dialog--xl { max-width: 1140px; }

.tn-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 22px;
    border-bottom: 1px solid var(--tn-border);
}

.tn-modal-title {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--tn-text-primary);
}

.tn-modal-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    background: none;
    border: none;
    border-radius: var(--tn-radius);
    cursor: pointer;
    color: var(--tn-text-muted);
    transition: all var(--tn-transition);
}

.tn-modal-close:hover {
    background: var(--tn-bg-page);
    color: var(--tn-text-primary);
}

.tn-modal-body {
    padding: 22px;
}

.tn-modal-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--tn-space-sm);
    padding: 16px 22px;
    border-top: 1px solid var(--tn-border);
}

/* ═══════════════════════════════════════════════════════════
   Mobile — bottom sheet, not a full-screen takeover.

   The dialog docks to the bottom edge, keeps rounded top corners, and is
   sized BY ITS CONTENT (capped at 92dvh) instead of being stretched to
   100% height. A three-line confirm shouldn't blank the whole screen —
   that reads as a page navigation rather than a dialog, and it costs the
   user the context they were looking at. Reach for .tn-modal--full when a
   dialog genuinely IS a full-screen task (a multi-step form, a picker
   with a long list).

   dvh (not vh) so the sheet doesn't sit under mobile Safari/Chrome's
   collapsing URL bar; env(safe-area-inset-bottom) keeps the footer's
   actions clear of the home indicator on notched phones.
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
    .tn-modal-container {
        padding: 0;
        align-items: flex-end;
    }

    .tn-modal-dialog {
        max-width: 100%;
        max-height: 92dvh;
        border-radius: var(--tn-radius-xl) var(--tn-radius-xl) 0 0;
        animation: tnSheetUp 0.24s cubic-bezier(0.32, 0.72, 0, 1);
        /* The sheet itself is the scroll container only via its body, so the
           header/footer can stay pinned (see below). */
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    /* Grab handle — the affordance that says "this sheet is dismissible".
       Drawn on the header so it needs no extra markup. */
    .tn-modal-header {
        position: relative;
        flex-shrink: 0;
        padding-top: 26px;
    }

    .tn-modal-header::before {
        content: "";
        position: absolute;
        top: 10px;
        left: 50%;
        transform: translateX(-50%);
        width: 36px;
        height: 4px;
        border-radius: 999px;
        background: var(--tn-border);
    }

    /* Below the 44px touch floor at the desktop 32px size, with no
       override here to catch it — and on mobile this button is the ONLY
       guaranteed exit from the dialog. A centred card can be dismissed by
       tapping outside; a bottom sheet or full-screen takeover has no
       "outside", and a drag handle is a gesture, not an accessible
       control. touch-action:manipulation removes the double-tap-zoom
       delay on what is, for this button specifically, a genuinely
       critical tap. */
    .tn-modal-close {
        width: var(--tn-touch-target);
        height: var(--tn-touch-target);
        touch-action: manipulation;
    }

    /* Only the body scrolls, so the title stays readable and the actions
       stay reachable no matter how long the content is. */
    .tn-modal-body {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* A .tn-alert rendered at the top of the body (a save/validation error)
       needs to stay visible while the body scrolls under it. The body is
       the ONLY scroll container on mobile — header/footer are pinned above
       — so a message that renders once, at the top, on a tall form has
       nothing keeping it on screen once the user scrolls down toward the
       footer's Save button. Found by scaneasy-agent (WMS #685): a tall
       bottom sheet (SupplierSettings) scrolled to its last fields showed
       Cancel+Save with no error visible anywhere, confirmed by
       elementFromPoint at the alert's centre returning null (outside the
       viewport, not merely covered) — toBeVisible() alone passed against
       both the working and the broken state. Two-field modals never
       surfaced this because they never scroll. Direct-child selector so a
       .tn-alert used deliberately inside a card or list elsewhere in the
       body isn't pinned unexpectedly. Desktop is unaffected: there the
       whole dialog scrolls as one flow (no separate pinned header), so
       this rule only applies inside the mobile bottom-sheet layout above. */
    .tn-modal-body > .tn-alert {
        position: sticky;
        top: 0;
        z-index: 1;
        background: var(--tn-bg-card);
    }

    /* Safe-area floor for dialogs with NO footer. .tn-modal-footer pads
       itself below, but most confirms put their actions in the body, not
       a footer — those dialogs had nothing protecting their last content
       from the iOS home indicator, especially at height:100dvh
       (.tn-modal-dialog--full) where the body IS the bottom edge, not
       just scrolled content above a safe footer. :has() scoping means
       this only fires when there's genuinely no footer to double up
       with, rather than every dialog getting extra bottom padding. */
    .tn-modal-dialog:not(:has(.tn-modal-footer)) .tn-modal-body {
        padding-bottom: calc(22px + env(safe-area-inset-bottom, 0px));
    }

    .tn-modal-footer {
        flex-shrink: 0;
        padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    }

    /* Full-width, comfortably tappable actions — a right-aligned pair of
       small buttons is a desktop idiom that fights the thumb on a phone. */
    .tn-modal-footer .tn-btn {
        flex: 1;
        min-height: var(--tn-touch-target);
    }

    /* Opt back in to the full-screen takeover for genuinely page-sized tasks */
    .tn-modal--full .tn-modal-dialog,
    .tn-modal-dialog.tn-modal-dialog--full {
        max-height: 100dvh;
        height: 100dvh;
        border-radius: 0;
    }

    .tn-modal--full .tn-modal-header::before,
    .tn-modal-dialog--full .tn-modal-header::before {
        display: none;
    }

    .tn-modal--full .tn-modal-header,
    .tn-modal-dialog--full .tn-modal-header {
        padding-top: 18px;
    }
}

@keyframes tnSheetUp {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
}

/* A sheet sliding up is motion some users need suppressed */
@media (prefers-reduced-motion: reduce) {
    .tn-modal-container,
    .tn-modal-dialog {
        animation: none;
    }
}
