/* Phone fit: nothing on a page may be wider than the screen.
   Loaded last on purpose. These are app-wide rules, so a page does not have to remember them one by one.
   - Form fields never grow past their box (iPhone date and time boxes have a wide built-in size and ignore the
     width of the form they sit in unless told otherwise).
   - On a phone, fields side by side in a form row get to shrink, and the page itself never scrolls sideways. */

#app input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
#app select,
#app textarea { max-width: 100%; box-sizing: border-box; }

#app input[type="date"], #app input[type="time"], #app input[type="datetime-local"], #app input[type="month"], #app input[type="week"] { min-width: 0; }

/* ===== Lists on a phone =====
   Tables and list rows that label their cells (data-label) become one tidy card per record, the same everywhere:
   the first cell is the title across the full width; every other cell is one short line with its label on the left and the value on the right;
   empty cells are left out (js/phone-fit.js); buttons sit on their own line at the bottom. Cards have the same padding and gap on every page. */
@media (max-width: 600px) {
    #app .page-content table:has(td[data-label]) { display: block !important; width: 100% !important; min-width: 0 !important; border: 0 !important; background: transparent !important; box-shadow: none !important; }
    #app .page-content table:has(td[data-label]) thead { display: none !important; }
    #app .page-content table:has(td[data-label]) tbody { display: grid !important; gap: 8px; width: 100% !important; min-width: 0 !important; }
    #app .page-content table:has(td[data-label]) tr,
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) {
        display: block !important; position: relative; width: auto !important; min-width: 0 !important; height: auto !important; margin: 0 !important;
        padding: 10px 12px !important; border: 1px solid var(--km2-border, rgba(128, 128, 128, .28)) !important; border-radius: 10px !important;
        background: var(--km2-surface, #fff) !important; box-shadow: none !important; grid-template-columns: none !important;
    }
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) + :is(div, a, li, article)[class]:has(> [data-label]) { margin-top: 8px !important; }
    #app .page-content table:has(td[data-label]) td[data-label],
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > [data-label] {
        display: block !important; width: auto !important; min-width: 0 !important; margin: 0 !important; padding: 3px 0 !important; border: 0 !important;
        background: transparent !important; text-align: right !important; font-size: .84rem !important; line-height: 1.4 !important; overflow-wrap: anywhere;
    }
    #app .page-content table:has(td[data-label]) td[data-label]::before,
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > [data-label]::before {
        content: attr(data-label) !important; display: block !important; float: left; max-width: 48%; margin: 0 12px 0 0 !important; padding: 0 !important;
        color: var(--km2-muted, #687871) !important; -webkit-text-fill-color: currentColor; font-size: .74rem !important; font-weight: 500 !important; letter-spacing: normal !important; text-transform: none !important; text-align: left; line-height: 1.6 !important;
    }
    /* The first cell is the record's name: full width, no label, a little larger. */
    #app .page-content table:has(td[data-label]) tr > td[data-label]:first-child,
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > [data-label]:first-child {
        text-align: left !important; font-size: .92rem !important; font-weight: 600; padding: 0 0 6px !important; margin-bottom: 4px !important; border-bottom: 1px solid var(--km2-border, rgba(128, 128, 128, .2)) !important;
    }
    #app .page-content table:has(td[data-label]) tr > td[data-label]:first-child::before,
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > [data-label]:first-child::before { content: none !important; display: none !important; }
    #app .page-content table:has(td[data-label]) tr > td[data-label]:first-child { display: flex !important; flex-wrap: wrap; align-items: center; justify-content: flex-start; gap: 4px 8px; }
    /* A leading pick box (select this row): a small box in the corner, and the next cell becomes the title. */
    #app .page-content table:has(td[data-label]) tr > td[data-label]:first-child:has(> input[type="checkbox"]:only-child) {
        float: left; display: block !important; width: auto !important; padding: 2px 10px 0 0 !important; margin: 0 !important; border: 0 !important;
    }
    #app .page-content table:has(td[data-label]) tr > td[data-label]:first-child:has(> input[type="checkbox"]:only-child) + td[data-label] {
        text-align: left !important; font-size: .92rem !important; font-weight: 600; padding: 0 0 6px !important; margin-bottom: 4px !important; border-bottom: 1px solid var(--km2-border, rgba(128, 128, 128, .2)) !important;
    }
    #app .page-content table:has(td[data-label]) tr > td[data-label]:first-child:has(> input[type="checkbox"]:only-child) + td[data-label]::before { content: none !important; display: none !important; }
    /* Cells that hold buttons or menus: their own line at the bottom, buttons side by side on the right. */
    #app .page-content table:has(td[data-label]) td[data-label]:not(:first-child):has(button, select, .km-button),
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > [data-label]:not(:first-child):has(button, select) {
        display: flex !important; flex-wrap: wrap; justify-content: flex-end; gap: 6px; padding-top: 8px !important; margin-top: 4px !important; border-top: 1px dashed var(--km2-border, rgba(128, 128, 128, .25)) !important;
    }
    #app .page-content table:has(td[data-label]) td[data-label]:not(:first-child):has(button, select, .km-button)::before,
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > [data-label]:not(:first-child):has(button, select)::before { content: none !important; display: none !important; }
    /* An "Actions" cell that only holds a link (Open →): no label, the link on the right. */
    #app .page-content table:has(td[data-label]) td:is([data-label="Actions"], [data-label="Action"], [data-label="Options"]) { text-align: right !important; padding-top: 6px !important; }
    #app .page-content table:has(td[data-label]) td:is([data-label="Actions"], [data-label="Action"], [data-label="Options"])::before { content: none !important; display: none !important; }
    /* Nothing recorded: left out (marked by js/phone-fit.js). */
    #app .page-content table:has(td[data-label]) td[data-label].pf-empty,
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > [data-label].pf-empty { display: none !important; }
    #app .page-content table:has(td[data-label]) td[data-label] :is(button, .km-button, select) { width: auto !important; min-height: 32px; padding: 4px 12px !important; font-size: .8rem !important; }
    #app .page-content table:has(td[data-label]) td[data-label] select { max-width: 60%; }
    /* A loose button in a list row (for example "close this ticket") sits in the top right corner. */
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]) > button:not([data-label]) { position: absolute !important; top: 8px; right: 8px; width: 30px !important; height: 30px !important; min-height: 0 !important; padding: 0 !important; }
    #app .page-content :is(div, a, li, article)[class]:has(> [data-label]):has(> button:not([data-label])) > [data-label]:first-child { padding-right: 36px !important; }
}

/* The Smart strip's "N more" button is for phones; a wider screen shows every line. */
#app .sg .sg-moreb { display: none; }

/* The phone's More sheet: a row of section chips under the shortcuts; a tap scrolls to that section. */
.mobile-more-jump { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 14px 4px; }
.mobile-more-jump button { padding: 5px 11px; border: 1px solid rgba(255, 255, 255, .16); border-radius: 999px; background: rgba(255, 255, 255, .06); color: rgba(255, 255, 255, .88); font: inherit; font-size: .74rem; font-weight: 600; letter-spacing: normal; text-transform: none; cursor: pointer; }
.mobile-more-jump button:active { background: rgba(255, 255, 255, .14); }
.mobile-more-destinations section { scroll-margin-top: 6px; }

/* iPhone and iPad (Safari and every browser there): draw date and time boxes like the other fields so they take the width they are given. */
@supports (-webkit-touch-callout: none) {
    #app input[type="date"], #app input[type="time"], #app input[type="datetime-local"], #app input[type="month"], #app input[type="week"] {
        -webkit-appearance: none; appearance: none; min-height: 2.4em; text-align: left;
    }
    #app input::-webkit-date-and-time-value { text-align: left; margin: 0; }
}
/* An iPhone zooms the whole page in when you tap a field whose text is smaller than 16px, and the page then looks wider than the screen
   and stays zoomed. Phone-size fields therefore use 16px text there. */
@supports (-webkit-touch-callout: none) {
    @media (max-width: 760px) {
        #app input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]), #app select, #app textarea { font-size: 16px !important; }
    }
}

@media (max-width: 760px) {
    #app .page-content { max-width: 100vw; overflow-x: clip; }
    /* Labels and field wrappers inside grids and flex rows may shrink to the column they are in. */
    #app .page-content label, #app .page-content fieldset, #app .page-content .form-group, #app .page-content .field, #app .page-content .form-row > * { min-width: 0; max-width: 100%; }
    #app .page-content fieldset { margin-inline: 0; }
    /* A date or time box inside a label, form or dialog fills its column instead of keeping its desktop size. */
    #app .page-content label input[type="date"], #app .page-content label input[type="time"], #app .page-content label input[type="datetime-local"], #app .page-content label input[type="month"],
    #app form input[type="date"], #app form input[type="time"], #app form input[type="datetime-local"], #app form input[type="month"],
    #app [role="dialog"] input[type="date"], #app [role="dialog"] input[type="time"], #app [role="dialog"] input[type="datetime-local"], #app [role="dialog"] input[type="month"] { width: 100%; }
    /* Long words and addresses wrap instead of pushing the page wider. */
    #app .page-content h1, #app .page-content h2, #app .page-content h3, #app .page-content p { overflow-wrap: anywhere; }
    /* The strip of pages under the top bar: on a phone every page of the area sits in one row you can swipe, instead of a few links and a "More" menu. */
    #app .module-navigation .module-navigation-links { flex: 1 1 auto; min-width: 0; display: flex !important; flex-wrap: nowrap; gap: 4px; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch;
        -webkit-mask-image: linear-gradient(to right, #000 88%, transparent); mask-image: linear-gradient(to right, #000 88%, transparent); padding-right: 18px; }
    #app .module-navigation .module-navigation-links::-webkit-scrollbar { display: none; }
    #app .module-navigation .module-navigation-links > a[hidden] { display: inline-flex !important; }
    #app .module-navigation .module-navigation-links > a { flex: 0 0 auto; white-space: nowrap; }
    #app .module-navigation .module-more { display: none !important; }
    /* Less clutter at the top of every page: the line under a page title is cut to two lines, and the Smart strip is left out when it only says that all is well. */
    #app .page-content h1 + p:not(.sx-headline), #app .page-content .it-heading p, #app .page-content .km-page-header p, #app .page-content .page-header p {
        display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-bottom: 8px;
    }
    #app .page-content .sg.sg-calm { display: none !important; }
    #app .page-content .sg:not(.sg-open) .sg-more { display: none !important; }
    #app .page-content .sg { flex-wrap: wrap; gap: 6px; }
    /* "N more" exists only on a phone. */
    #app .page-content .sg .sg-moreb { display: inline-flex !important; }
    /* Readable small print: notes and meta lines get room to breathe. */
    #app .page-content small { line-height: 1.45; }
    /* Tab rows that scroll sideways fade at the edge, so it is clear there is more to see. */
    #app .page-content :is(.page-section-tabs, .sx-tabs, .doc-tabs, .team-tabs, .employee-tabs, .account-tabs, .portfolio-tabs, .customer-profile-tabs, .site-service-tabs) {
        -webkit-mask-image: linear-gradient(to right, #000 85%, transparent); mask-image: linear-gradient(to right, #000 85%, transparent); padding-right: 24px;
    }
    /* Cybersecurity client page: the summary line, then the two PDFs side by side as small buttons; the "where this client stands" tiles as an even grid. */
    #app .sx-headline { font-size: .86rem; line-height: 1.5; }
    #app .sx-headline .sx-link { display: inline-flex; margin: 8px 6px 0 0; padding: 5px 10px; border: 1px solid rgba(128, 128, 128, .4); border-radius: 8px; font-size: .78rem; text-decoration: none; }
    #app .sx-board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
    #app .sx-board .sx-stat { min-width: 0; padding: 7px 8px; border-radius: 9px; }
    #app .sx-board .sx-stat strong { font-size: .95rem; overflow-wrap: anywhere; }
    #app .sx-board .sx-stat span { font-size: .68rem; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
    /* Table rows shown as cards: cells with nothing recorded ("-") are left out (js/phone-fit.js marks them), so each card shows only what is there. */
    #app .page-content td.pf-empty { display: none !important; }
    /* A page header with the title on one side and buttons on the other: stacked on a phone, title first, the buttons across the full width under it. */
    #app .page-content :is(header, div)[class*="header"]:has(> div > h1):not(.module-navigation) { display: flex !important; flex-direction: column !important; align-items: stretch !important; gap: 10px !important; }
    #app .page-content :is(header, div)[class*="header"]:has(> div > h1) > * { min-width: 0; max-width: 100%; flex: 0 0 auto !important; }
    /* Dispatch calendar: the day's tools two to a row, then Previous, Today and Next together on one row; "Expand calendar" is for big screens. */
    #app .page-content .jobcalendar-header-actions { display: grid !important; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; width: 100%; }
    #app .page-content .jobcalendar-header-actions > * { grid-column: span 3; width: 100% !important; min-height: 36px; margin: 0 !important; padding: 6px 8px !important; font-size: .8rem !important; }
    #app .page-content .jobcalendar-header-actions > :nth-last-child(-n+3) { grid-column: span 2; }
    #app .page-content .jobcalendar-header-actions > .jobcalendar-bigscreen { display: none !important; }
    #app .page-content .dispatch-find { max-width: none; }
    /* Page header button rows: equal buttons of normal height, two to a row (Assets drew tall boxes). */
    #app .page-content .assets-action-row { display: grid !important; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; width: 100%; height: auto !important; }
    #app .page-content .assets-action-row > * { display: inline-flex !important; align-items: center; justify-content: center; width: 100% !important; height: auto !important; min-height: 38px !important; max-height: none !important; padding: 8px 10px !important; writing-mode: horizontal-tb !important; white-space: normal; text-align: center; }
    #app .page-content .assets-action-row > .assets-primary-btn { grid-column: 1 / -1; }
    /* Settings panels: no deep inset on a phone, the content gets the width. */
    #app .page-content .settings-panel { padding: 12px 10px !important; }
    /* Media never wider than its box. */
    #app .page-content img, #app .page-content video, #app .page-content canvas, #app .page-content svg { max-width: 100%; }
}

/* Links inside a sentence are underlined, so they do not depend on colour alone to be seen (WCAG: links must be distinguishable without colour). */
#app .page-content :is(p, li, .sx-muted, .settings-muted, .dashboard-muted, .dashboard-today-empty) > a:not([class]),
#app .page-content :is(.sx-muted, .settings-muted, .dashboard-muted, .dashboard-today-empty) a:not([class]) { text-decoration: underline; text-underline-offset: 2px; }