/* Phone width never scrolls sideways (founder 2026-11-10: "scrolling
 * horizontally is bad UX — adapt all element sizes to the width of the
 * screen"). The agency pages are read in the app's webview at ~390px, so:
 *
 *   1. the page itself can never be dragged sideways (`clip`, not `hidden`:
 *      it creates no scroll container, so sticky headers keep working);
 *   2. a table that does not fit is re-drawn as one block per row — the first
 *      cell is the row's title, every other cell is "column name … value".
 *      phone-fit.js decides WHICH tables (only those that overflow) and writes
 *      each cell's column name into `data-label`;
 *   3. rows of tabs, chips and buttons wrap instead of scrolling.
 *
 * Opt out for a surface that must stay a grid (a calendar, a pasted e-mail):
 * `data-phone-table="keep"` on the table or any ancestor.
 * Value colours are inherited; the column name has one grey that reads on
 * the day and the night ground. */
@media (max-width: 639px) {
    html, body { overflow-x: clip; max-width: 100%; }

    /* media and embeds never set the page's width */
    img, video, canvas, svg[width], iframe, pre { max-width: 100%; }
    pre { white-space: pre-wrap; overflow-wrap: anywhere; }

    /* ── 2. tables that do not fit ─────────────────────────────────────── */
    table.tp-stack,
    table.tp-stack > tbody,
    table.tp-stack > tfoot,
    table.tp-stack > tbody > tr,
    table.tp-stack > tfoot > tr {
        display: block;
        width: 100% !important;
        min-width: 0 !important;
    }
    table.tp-stack > colgroup { display: none; }
    /* the header row is spoken, not shown: its words sit beside each value */
    table.tp-stack > thead {
        position: absolute; width: 1px; height: 1px; overflow: hidden;
        clip-path: inset(50%); white-space: nowrap;
    }
    table.tp-stack > tbody > tr,
    table.tp-stack > tfoot > tr { padding: 12px 0; }
    /* a table that sat flush in its frame (a card without padding) lived off
       its cells' padding, which is gone here: the row carries it instead
       (phone-fit.js sets the class by measurement) */
    table.tp-stack.tp-stack-inset > tbody > tr,
    table.tp-stack.tp-stack-inset > tfoot > tr { padding: 14px 16px; }
    table.tp-stack > tbody > tr > td,
    table.tp-stack > tbody > tr > th,
    table.tp-stack > tfoot > tr > td,
    table.tp-stack > tfoot > tr > th {
        /* name on the left, value(s) on the right; a value that does not fit
           beside its name goes under it, still on the right */
        display: flex; flex-wrap: wrap; align-items: baseline; justify-content: flex-end;
        gap: 4px 12px;
        position: static !important;
        width: auto !important; min-width: 0 !important; max-width: none !important;
        padding: 4px 0 !important; border: 0 !important;
        text-align: right !important; white-space: normal !important;
        overflow-wrap: anywhere;
    }
    table.tp-stack > tbody > tr > td::before,
    table.tp-stack > tfoot > tr > td::before {
        content: attr(data-label);
        flex: 0 1 auto; max-width: 50%; margin-right: auto;
        text-align: left; font-size: 11px; font-weight: 600; line-height: 1.5;
        letter-spacing: .04em; text-transform: uppercase;
        /* its own grey, never the value's colour (a green amount had a green name) */
        color: #7b7485;
    }
    table.tp-stack > tbody > tr > td > *,
    table.tp-stack > tfoot > tr > td > * { min-width: 0; max-width: 100%; }
    /* nothing inside a cell is cut off: a long name or address wraps */
    table.tp-stack td *:not(svg):not(path) {
        white-space: normal !important; text-overflow: clip !important;
    }
    /* the row's title: first cell, full width, no column name */
    table.tp-stack > tbody > tr > :first-child,
    table.tp-stack > tbody > tr > [data-phone-title],
    table.tp-stack > tbody > tr > [data-phone-wide],
    table.tp-stack > tfoot > tr > [data-phone-wide] {
        display: block; text-align: left !important; font-weight: 600;
        padding-bottom: 6px !important;
    }
    table.tp-stack > tbody > tr > :first-child::before,
    table.tp-stack > tbody > tr > [data-phone-title]::before,
    table.tp-stack > tbody > tr > [data-phone-wide]::before,
    table.tp-stack > tfoot > tr > [data-phone-wide]::before,
    table.tp-stack td[data-label=""]::before,
    table.tp-stack td:not([data-label])::before { content: none; }
    table.tp-stack > tbody > tr > [data-phone-wide]:not(:first-child) { font-weight: inherit; }
    /* a long text value (a sentence, a query): its name stands over it and it
       reads from the left like a paragraph (phone-fit.js marks it) */
    table.tp-stack > tbody > tr > td[data-phone-long] {
        display: block; text-align: left !important;
    }
    table.tp-stack > tbody > tr > td[data-phone-long]::before {
        display: block; max-width: none; margin: 0 0 2px;
    }
    /* a select-box column: the box stands beside the row's title (the next
       cell), not alone on a line of its own */
    table.tp-stack > tbody > tr.tp-row-check {
        display: grid; grid-template-columns: auto minmax(0, 1fr);
        column-gap: 12px; align-items: center;
    }
    table.tp-stack > tbody > tr.tp-row-check > * { grid-column: 1 / -1; }
    table.tp-stack > tbody > tr.tp-row-check > [data-phone-check] {
        grid-column: 1; display: block; padding: 0 0 6px !important; font-weight: inherit;
    }
    /* its own lines keep their own weight (a name is bold, the address is not) */
    table.tp-stack > tbody > tr.tp-row-check > [data-phone-title] { grid-column: 2; font-weight: inherit; }
    table.tp-stack > tbody > tr.tp-row-check > [data-phone-check]:empty { display: none; }
    table.tp-stack > tbody > tr.tp-row-check > [data-phone-check]:empty + [data-phone-title] { grid-column: 1 / -1; }
    /* a cell with nothing in it is not a line */
    table.tp-stack td:empty { display: none; }
    /* its old scroll box has nothing left to scroll */
    .tp-stack-box { overflow-x: visible !important; }

    /* ── 3. strips wrap ───────────────────────────────────────────────── */
    .tp-wrap-strip {
        flex-wrap: wrap !important; overflow-x: visible !important;
        white-space: normal !important; row-gap: 6px;
    }
    .tp-wrap-strip > * { max-width: 100%; white-space: normal; scroll-snap-align: none; }

    /* a row that reached past the screen (phone-fit.js, by measurement) */
    .tp-wrap-row { flex-wrap: wrap !important; row-gap: 8px; }
    .tp-wrap-row > * { max-width: 100%; min-width: 0; }
    /* a part that "takes the rest" starts from its own width here, or it would
       still be squeezed to nothing on the first line */
    .tp-wrap-row > .flex-1 { flex-basis: auto; }
    /* a grid column is never wider than the screen because of one long field
       or word inside it; a field never sets its box's width */
    .grid > * { min-width: 0; }
    select, input, textarea { max-width: 100%; }
    /* break-word, not anywhere: a title must not shrink to one letter per line
       beside its buttons — it keeps its width and the row wraps instead */
    h1, h2, h3 { overflow-wrap: break-word; }

    /* ── 4. named cases ───────────────────────────────────────────────── */
    /* a board of columns (leads, campaigns): the columns stand under each other */
    .tp-phone-col { flex-direction: column !important; min-width: 0 !important; }
    .tp-phone-col > * { width: 100% !important; }
    /* a side panel that pushes the page (manager Spotty, 420px): covers the screen while open */
    .tp-phone-panel.tp-phone-open {
        position: fixed; inset: 0; z-index: 60; width: 100% !important;
    }
    .tp-phone-panel.tp-phone-open > .tp-phone-panel-inner { width: 100% !important; border-left: 0; }
    /* a bar pinned over its field (Finder's example sentences): in the flow, sentences wrap */
    .tp-phone-static { position: static !important; align-items: flex-start !important; gap: 8px !important; }
    .tp-phone-static > * { height: auto !important; text-align: left; }
    table.tp-stack { table-layout: auto !important; }
    table.tp-stack .sticky { position: static !important; }
}

/* ── 5. overlays never slide sideways (ticket 85557621, 2026-11-22) ──────
 * A modal is a fixed full-screen root (`fixed inset-0 overflow-y-auto`, or
 * the keyboard-aware `tp-sheet`) with a panel that scrolls (`overflow-y-auto`).
 * With only overflow-y set the browser makes overflow-x `auto` as well, so one
 * child that reaches past the panel by a pixel (a tab row that cannot shrink,
 * a long unbroken word) made the whole sheet pan left and right under a finger.
 * Offenders are fixed where they are; this guard makes sure the next one
 * cannot move the sheet: no sideways scroll box (`clip`, `hidden` where clip is
 * unknown — next to a scrolling y both compute to hidden), no sideways
 * rubber-band or hand-off to the page behind, and a finger only scrolls up and
 * down (pinch-zoom stays: WCAG 1.4.4).
 * Phones AND touch screens of any width (an iPad runs the app): a desktop with
 * a mouse is untouched. A strip that is meant to slide keeps sliding: it is its
 * own scroll box (`overflow-x-auto` / `overflow-auto` / data-phone-strip="keep"),
 * which the guard skips, and it is given the sideways pan back explicitly.
 * Hooks: any root/panel of that shape, plus `data-overlay-root` /
 * `data-overlay-panel` on the shared modal frames (components/modal_base.html,
 * the listing editor and preview). Plain CSS on purpose (no Tailwind rebuild).
 * phone-fit.js undoes a sideways shift that a focus() or scrollIntoView leaves
 * behind in such a box (`hidden` can still be moved by script, not by a finger). */
@media (max-width: 639px), (pointer: coarse) {
    .fixed.inset-0.overflow-y-auto,
    .tp-sheet.overflow-y-auto,
    [data-overlay-root],
    :is(.fixed.inset-0.overflow-y-auto, .tp-sheet, #modal-container) .overflow-y-auto:not(.overflow-x-auto):not(.overflow-x-scroll):not([data-phone-strip="keep"]),
    [data-overlay-panel] {
        overflow-x: hidden;
        overflow-x: clip;
        overscroll-behavior-x: none;
        touch-action: pan-y pinch-zoom;
    }
    :is(.fixed.inset-0.overflow-y-auto, .tp-sheet, #modal-container, [data-overlay-root]) :is(.overflow-x-auto, .overflow-x-scroll, .overflow-auto, [data-phone-strip="keep"]) {
        touch-action: pan-x pan-y pinch-zoom;
        overscroll-behavior-x: contain;
    }
}

/* Date and time fields stay inside their column (ticket 9b27790a, 2026-11-23;
 * the event forms had this one by one since 693562fe). iOS Safari draws a date
 * field as a native pill with an intrinsic width that ignores `width` and
 * `min-width`, so on a phone every date field without the fix ran past the
 * card's right edge (Track's Date Applied / Deadline, Write's tracking
 * panel, Pocket CRM's follow-up, the task due date, Circle events, the
 * portfolio birth date…). `appearance: none` lets it take the column width;
 * the field keeps its own border, so an empty one still reads as a field.
 * Zero specificity (`:where`), so a field's own classes still win. The field's
 * HEIGHT is not touched (a min-height here would beat a small `h-8` field):
 * an EMPTY date has no value text and so no height on iOS — the value box gets
 * one line of height instead, inside whatever height the field sets. The
 * picker itself is the phone's own sheet and is not ours to size. */
@media (max-width: 639px) {
    :where(input[type="date"], input[type="datetime-local"], input[type="time"], input[type="month"], input[type="week"]) {
        box-sizing: border-box;
        max-width: 100%;
        min-width: 0;
        -webkit-appearance: none;
        appearance: none;
    }
    /* WebKit only (other engines drop this rule): one line of height for an empty value. */
    :where(input[type="date"], input[type="datetime-local"], input[type="time"], input[type="month"], input[type="week"])::-webkit-date-and-time-value {
        min-height: 1.5em;
    }
}
