/**
 * DH Suite — the phone layout.
 *
 * What a page looks like under 768px, where the phone bar replaces the menu
 * (sidebar.css). Shared pieces only; a page's own sheet handles its own
 * blocks. The rules every page follows (STYLE-GUIDE.md, "Phones"):
 *
 *   - Nothing is wider than the screen. Wide content scrolls inside its own
 *     box, never the page.
 *   - Controls in a row share it evenly: a wrapped row looks planned.
 *   - Sizes are rem, so the page grows with the phone's text size
 *     (suite_base.html's text-scale tag, reset.css for iPhones).
 *   - Targets are at least 2.75rem (44px at the default size) tall.
 *
 * The list controls (js/phone-controls.js): a bar with the search, a
 * Filters button and a More button; two sheets that rise from the bottom;
 * the page's main action as a floating button above the phone bar.
 */

@layer components {

/* The bar, the title's More, the context line and the sheets' parts exist
   only on a phone */
.phone-controls[hidden],
.phone-controls-count[hidden],
.phone-controls-btn[hidden],
.phone-more-btn[hidden],
.phone-context[hidden] {
    display: none;
}

@media (max-width: 767px) {

    /* --- The title, with More beside it --------------------------------- */

    /* At the largest text sizes a long title leaves no room: More then
       goes under it, at the right */
    .phone-title-row {
        flex: 1 1 100%;
        width: 100%;
        min-width: 0;
        flex-wrap: wrap;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--spacing-2);
        margin-bottom: var(--spacing-2);
    }

    .phone-title-row h1 {
        flex: 1 1 auto;
        min-width: 0;
        margin: 0;
    }

    /* Icon and word: the word says what it does */
    .phone-more-btn {
        flex: 0 0 auto;
        margin-left: auto;
        gap: var(--spacing-1);
        min-height: 2.75rem;
        padding: var(--spacing-1) var(--spacing-3);
        white-space: nowrap;
    }

    /* --- Where the page is: "Hurricane Test · Peace ▾" ------------------- */

    .phone-context {
        display: flex;
        align-items: center;
        gap: var(--spacing-1);
        max-width: 100%;
        min-height: 2.75rem;
        margin: 0 0 var(--spacing-2);
        padding: 0;
        background: none;
        border: 0;
        font: inherit;
        font-weight: var(--font-weight-semibold);
        color: var(--color-primary);
        text-align: left;
        cursor: pointer;
    }

    .phone-context-text {
        display: flex;
        gap: var(--spacing-1);
        min-width: 0;
    }

    .phone-context-name {
        flex: 0 1 auto;
        min-width: 2.5rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .phone-context-sep {
        flex: none;
        color: var(--color-gray-500);
    }

    .phone-context-chevron {
        display: inline-flex;
        flex: none;
    }

    .phone-context:focus-visible {
        outline: var(--focus-ring);
        outline-offset: var(--focus-ring-offset);
        border-radius: var(--radius-sm);
    }

    /* --- The search row: the search, and Filters at its end -------------- */

    .phone-controls {
        display: flex;
        flex-wrap: wrap;
        align-items: stretch;
        gap: var(--spacing-2);
        margin-bottom: var(--spacing-3);
    }

    /* Beside Filters while it has room (any text size up to about 160%);
       above it at the largest sizes */
    .phone-controls-search {
        flex: 1 1 9rem;
        min-width: 0;
        display: flex;
    }

    /* Whatever search box the page has fills the slot */
    .phone-controls-search > * {
        flex: 1 1 auto;
        width: 100%;
        min-width: 0;
        max-width: none;
        margin: 0;
    }

    .phone-controls-search input {
        min-width: 0;
        width: 100%;
    }

    /* The box says Search; its label stays for screen readers */
    .phone-controls-search label {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0 0 0 0);
        white-space: nowrap;
    }

    /* Compact view and column choices are for a wide table; a phone shows
       list rows (js/phone-controls.js hides them, past any page's style) */
    #density-toggle,
    #column-toggle-btn {
        display: none;
    }

    .phone-controls-btn {
        position: relative;
        flex: 0 0 auto;
        gap: var(--spacing-1);
        min-height: 2.75rem;
        padding: var(--spacing-1) var(--spacing-3);
        white-space: nowrap;
    }

    /* No search: Filters alone, at its own width */
    .phone-controls-search:empty {
        display: none;
    }

    /* Neither: the row takes no room */
    .phone-controls.is-search-only:has(.phone-controls-search:empty) {
        display: none;
    }

    .phone-controls-count {
        min-width: 1.25rem;
        padding: 0 var(--spacing-1);
        border-radius: var(--radius-full);
        background-color: var(--color-primary);
        color: var(--color-on-brand, white);
        font-size: var(--font-size-xs);
        font-weight: var(--font-weight-bold);
        line-height: 1.25rem;
        text-align: center;
    }

    /* --- The floating "add" ---------------------------------------------- */
    /* Labeled at the top of the page; a round "+" while it scrolls down, so
       it covers less of the list (js/phone-controls.js, `is-compact`) */

    .phone-fab {
        position: fixed;
        right: calc(var(--spacing-4) + env(safe-area-inset-right, 0px));
        bottom: calc(56px + var(--spacing-4) + env(safe-area-inset-bottom, 0px));
        z-index: 90;
        max-width: calc(100vw - 2 * var(--spacing-4));
    }

    .phone-fab > .btn {
        gap: var(--spacing-2);
        min-height: 3.25rem;
        min-width: 3.25rem;
        padding: var(--spacing-2) var(--spacing-4) var(--spacing-2) var(--spacing-3);
        border-radius: var(--radius-full);
        box-shadow: var(--shadow-lg);
        white-space: nowrap;
    }

    .phone-fab-icon {
        display: inline-flex;
        flex: none;
    }

    .phone-fab.is-compact > .btn {
        width: 3.5rem;
        height: 3.5rem;
        min-height: 0;
        padding: 0;
    }

    .phone-fab.is-compact .phone-fab-label {
        display: none;
    }

    @media (prefers-reduced-motion: no-preference) {
        .phone-fab > .btn {
            transition: padding var(--transition-fast);
        }
    }

    /* Room under the last row, so the floating button never covers it */
    body.has-phone-fab .app-main {
        padding-bottom: calc(var(--spacing-6) + 56px + 4.5rem + env(safe-area-inset-bottom, 0px));
    }
}

/* --- The sheets --------------------------------------------------------- */
/* A <dialog> opened with showModal(); on a phone it rises from the bottom,
   within thumb reach, and on a wide screen it is a small centered box. */

.phone-sheet {
    width: min(32rem, 100vw - 2rem);
    max-height: 85vh;
    max-height: 85dvh;
    padding: 0;
    border: 0;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    overscroll-behavior: contain;
}

.phone-sheet[open] {
    display: flex;
    flex-direction: column;
}

.phone-sheet::backdrop {
    background-color: rgba(0, 0, 0, 0.45);
}

.phone-sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--spacing-3);
    padding: var(--spacing-3) var(--spacing-4);
    border-bottom: 1px solid var(--color-gray-200);
}

.phone-sheet-head h2 {
    margin: 0;
    font-size: var(--font-size-lg);
}

.phone-sheet-head .btn {
    min-height: 2.75rem;
    min-width: 5rem;
}

.phone-sheet-body {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-3);
    padding: var(--spacing-4);
    padding-bottom: calc(var(--spacing-4) + env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
}

/* Each control is one full-width row */
.phone-sheet-body > *,
.phone-sheet-body select,
.phone-sheet-body input:not([type="checkbox"]):not([type="radio"]) {
    width: 100%;
    max-width: none;
    min-width: 0;
    margin: 0;
}

.phone-sheet-body select,
.phone-sheet-body input:not([type="checkbox"]):not([type="radio"]) {
    min-height: 2.75rem;
    font-size: var(--font-size-base);
}

.phone-sheet-body .form-group {
    margin: 0;
}

/* A page's group of filters stacks in the sheet */
.phone-sheet-body > div:not(.phone-sheet-field) {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: var(--spacing-3);
}

.phone-sheet-field {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-1);
}

.phone-sheet-caption {
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-semibold);
    color: var(--color-gray-700);
}

/* The More sheet: a list of actions, like a phone's share sheet */
.phone-sheet-actions .phone-sheet-body {
    gap: var(--spacing-2);
}

.phone-sheet-actions .phone-sheet-body > .btn,
.phone-sheet-actions .phone-sheet-body > * > .btn {
    justify-content: flex-start;
    width: 100%;
    min-height: 2.75rem;
    white-space: normal;
    text-align: left;
}

/* A select and its button (download format, then Download) share a row */
.phone-sheet-actions .phone-sheet-body > div {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: stretch;
}

.phone-sheet-actions .phone-sheet-body > div > select {
    flex: 1 1 8rem;
    width: auto;
}

.phone-sheet-actions .phone-sheet-body > div > .btn {
    flex: 1 1 8rem;
    width: auto;
    justify-content: center;
}

.phone-sheet-name {
    margin-left: var(--spacing-2);
}

@media (max-width: 767px) {
    .phone-sheet {
        width: 100%;
        max-width: none;
        margin: auto 0 0;
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    }
}

/* "Details shown": one checkbox per column, a full-width row each */
.phone-sheet-hint {
    margin: 0;
    font-size: var(--font-size-sm);
    color: var(--color-gray-600);
}

.phone-details-list {
    display: flex;
    flex-direction: column;
}

.phone-details-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-3);
    min-height: 2.75rem;
    border-bottom: 1px solid var(--color-gray-100);
    cursor: pointer;
}

.phone-details-item input {
    flex: none;
}

/* The More sheet's first entry opens it */
.phone-sheet-actions .phone-details-open {
    justify-content: flex-start;
}

/* --- The page title ----------------------------------------------------- */
/* Pages whose title is only for screen readers on a wide screen (Respond's,
   whose response picker heads the page) show it on a phone, where nothing
   else says where you are. */

.page-title-phone {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

@media (max-width: 767px) {
    .page-title-phone {
        position: static;
        width: auto;
        height: auto;
        margin: 0 0 var(--spacing-3);
        overflow: visible;
        clip: auto;
        white-space: normal;
        font-size: var(--font-size-2xl);
        line-height: 1.2;
    }
}

/* --- General phone polish ----------------------------------------------- */

@media (max-width: 767px) {
    /* A page's title: smaller than a wide screen's, and a long word wraps
       rather than running off at a large text size */
    .app-main h1 {
        font-size: var(--font-size-2xl);
        line-height: 1.2;
        overflow-wrap: break-word;
    }

    /* "Expand" widens a page past 1,440px; a phone never is */
    .page-width-bar {
        display: none;
    }

    /* Count chips (status counts over a list) stay on one row and scroll
       sideways, like a phone app's filter chips, instead of wrapping into
       uneven rows. The faded edge says there is more. */
    .phone-chips {
        display: flex;
        flex-wrap: nowrap;
        gap: var(--spacing-2);
        overflow-x: auto;
        overscroll-behavior-x: contain;
        scrollbar-width: none;
        margin-inline: calc(-1 * var(--spacing-4));
        padding-inline: var(--spacing-4);
        padding-bottom: var(--spacing-1);
        -webkit-mask-image: linear-gradient(to right, black 85%, transparent);
        mask-image: linear-gradient(to right, black 85%, transparent);
    }

    .phone-chips::-webkit-scrollbar {
        display: none;
    }

    .phone-chips > * {
        flex: 0 0 auto;
    }

    /* A row of tabs that does not fit scrolls sideways rather than running
       off the screen; each tab keeps its name on one line */
    .tabs,
    .filter-tabs,
    .plan-tabs,
    .tab-bar,
    [role="tablist"] {
        overflow-x: auto;
        overscroll-behavior-x: contain;
        scrollbar-width: none;
    }

    .tabs > *,
    .filter-tabs > *,
    .plan-tabs > *,
    .tab-bar > *,
    [role="tablist"] > * {
        flex-shrink: 0;
        white-space: nowrap;
    }

    /* No control is wider than its box: a select sized by its longest
       option, a file picker, a button with a long name (which wraps) */
    .app-main select,
    .app-main input,
    .app-main textarea,
    .app-main .btn {
        max-width: 100%;
    }

    .app-main .btn {
        white-space: normal;
    }

    /* A long code sample (a script variable) breaks rather than running
       off the screen */
    .app-main code {
        overflow-wrap: anywhere;
    }

    /* Count tiles: a long label wraps inside its tile */
    .stat-card {
        min-width: 0;
        overflow-wrap: break-word;
    }

    /* A record's fields: a long email or address wraps in its column */
    .detail-item,
    .detail-value {
        min-width: 0;
        overflow-wrap: break-word;
    }
}

/* --- Controls ----------------------------------------------------------- */

/* A select or text box without a form class (a toolbar's filter, a download
   format) looks like the rest of the suite rather than the browser's own.
   :where() keeps this at no specificity, so any page's rule wins. */
:where(.app-main, .phone-sheet) :where(
    select:not([multiple]),
    input:not([type]),
    input[type="text"], input[type="search"], input[type="date"],
    input[type="number"], input[type="email"], input[type="tel"],
    input[type="time"], input[type="url"]
) {
    padding: var(--spacing-2) var(--spacing-3);
    font: inherit;
    line-height: 1.5;
    color: var(--color-gray-900);
    background-color: white;
    border: 1px solid var(--color-border-input);
    border-radius: var(--radius-md);
}

/* A finger needs a bigger target than a mouse: 44px at the default text
   size (Apple's guidance; Material asks 48dp), growing with the text. */
@media (pointer: coarse) {
    .btn,
    select,
    input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="range"]),
    [role="tab"] {
        min-height: 2.75rem;
    }

    .btn-icon {
        min-width: 2.75rem;
    }

    /* The header is 60px whatever the text size (layout.css), and the page
       starts under it: its buttons stay compact so they fit inside it */
    .app-header .btn {
        min-height: 0;
        padding-block: var(--spacing-1);
        line-height: 1.2;
    }

    input[type="checkbox"],
    input[type="radio"] {
        width: 1.25rem;
        height: 1.25rem;
    }
}

@media (max-width: 767px) {
    /* Desktop padding (12px by 24px) made buttons wrap at very different
       widths; a phone's are tighter and wrap evenly */
    .btn:not(.btn-sm):not(.btn-lg):not(.btn-icon) {
        padding: var(--spacing-2) var(--spacing-4);
    }

    .btn-sm:not(.btn-icon) {
        padding: var(--spacing-1) var(--spacing-3);
    }

    .btn-lg:not(.btn-icon) {
        padding: var(--spacing-3) var(--spacing-5);
    }

    /* A row of buttons a page leaves in place: each takes an even share, so
       a wrapped row looks planned */
    .page-header-actions,
    .action-buttons,
    .form-actions,
    .modal-actions,
    .modal-footer {
        flex-wrap: wrap;
    }

    .page-header-actions > .btn,
    .page-header-row .action-buttons > .btn,
    .form-actions > .btn,
    .modal-actions > .btn,
    .modal-footer > .btn {
        flex: 1 1 8rem;
        white-space: normal;
    }

    .page-header-actions {
        width: 100%;
    }
}

} /* end @layer components */
