/*
 * ZE Platform - mobile / touch layer, loaded by every layout (partials/pwa-head).
 * Desktop looks stay as they are; everything below only applies on small screens or touch devices.
 * Breakpoints: <= 900px (tablet portrait and phones: menu becomes a drawer), <= 640px (phones: stacked lists, full-width
 * fields), <= 400px (small phones: tighter spacing).
 */

/* the top bar (name, menu, user) stays at the top while the page scrolls; in-page jumps leave room for it */
header.topbar { position: sticky; top: 0; z-index: 1000; box-shadow: 0 1px 0 var(--border, #e2e2e2); }
html { scroll-padding-top: 64px; }
@media (max-width: 900px) { html { scroll-padding-top: 120px; } }

/* ---- any size ---------------------------------------------------------------------------------------------- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
img, video, canvas, svg { max-width: 100%; height: auto; }
/* form controls use the page font (browsers default textareas and buttons to monospace / system fonts) */
input, select, textarea, button { font-family: inherit; }
/* the user's name in the top bar is a link, but should not look like a raw browser link */
header.topbar > span > a:not(.btn) { color: inherit; text-decoration: none; }
header.topbar > span > a:not(.btn):hover { text-decoration: underline; }
pre, code, .break-anywhere { overflow-wrap: anywhere; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
.table-scroll > table { min-width: 100%; }

/* notches and the home-indicator bar when the app is opened from the home screen */
@supports (padding: max(0px)) {
    header.topbar { padding-left: max(20px, env(safe-area-inset-left)); padding-right: max(20px, env(safe-area-inset-right)); padding-top: max(10px, env(safe-area-inset-top)); }
    main { padding-left: max(20px, env(safe-area-inset-left)); padding-right: max(20px, env(safe-area-inset-right)); padding-bottom: max(20px, env(safe-area-inset-bottom)); }
}

/* ---- touch devices: comfortable targets, no sticky hover ---------------------------------------------------- */
@media (pointer: coarse) {
    .btn, button, input[type=submit], input[type=button], select { min-height: 44px; }
    input[type=text], input[type=email], input[type=password], input[type=search], input[type=number], input[type=date], input[type=time], input[type=tel], input[type=url], textarea { min-height: 44px; }
    input[type=checkbox], input[type=radio] { width: 22px; height: 22px; }
    label { padding: 2px 0; }
    nav.mainmenu .mm-group > a, nav.mainmenu .mm-drop a { min-height: 44px; display: flex; align-items: center; }
    a { -webkit-tap-highlight-color: rgba(47, 122, 74, .18); }
    .apps a { padding: 16px; font-size: 16px; }
}

/* ---- tablet portrait and phones ---------------------------------------------------------------------------- */
@media (max-width: 900px) {
    header.topbar { flex-wrap: wrap; gap: 8px; padding-left: 12px; padding-right: 12px; }
    header.topbar > span { margin-left: auto; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
    main { padding: 14px 12px; }
    main.wide { max-width: 100%; }
    h1 { font-size: 22px; line-height: 1.25; margin: 4px 0 10px; }
    h2 { font-size: 18px; }
    /* inputs at 16px: iOS Safari zooms the page into any smaller field */
    input[type=text], input[type=email], input[type=password], input[type=search], input[type=number], input[type=date], input[type=time], input[type=tel], input[type=url], input[type=file], textarea, select { font-size: 16px; }
    .card { padding: 14px; }

    /* forms: every field fills the width, labels sit above their field, checkbox / radio rows are one comfortable line */
    main input:not([type=checkbox]):not([type=radio]):not([type=hidden]):not([type=submit]):not([type=button]):not([type=image]):not([type=range]):not([type=color]),
    main select, main textarea { width: 100%; max-width: 100%; display: block; margin-top: 4px; }
    main label { display: block; }
    main label:has(> input[type=checkbox]), main label:has(> input[type=radio]) { display: flex; align-items: center; gap: 10px; padding: 8px 0; font-size: 15px; color: var(--text, #1a1a1a); }
    main label > input[type=checkbox], main label > input[type=radio] { flex: 0 0 auto; margin: 0; }
    main textarea { min-height: 96px; }
    main form > .btn, main form button.btn { margin-top: 6px; }
}

/* ---- lists as cards: tablet portrait and phones -------------------------------------------------------------- */
@media (max-width: 900px) {
    /* lists that opt in with class "stack-sm" become cards (labels come from the header cells, see mobile.js) */
    table.stack-sm, table.stack-sm tbody, table.stack-sm tr, table.stack-sm td { display: block; width: 100%; }
    table.stack-sm thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
    table.stack-sm tr { border: 1px solid var(--border, #e2e2e2); border-radius: 10px; background: #fff; margin: 0 0 12px; padding: 4px 0; overflow: hidden; }
    table.stack-sm tbody tr:nth-child(odd), table.stack-sm tbody tr:nth-child(even) { background: #fff; }
    table.stack-sm td { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; text-align: right; padding: 7px 12px; border: 0; border-bottom: 1px solid #f0f0f0; word-break: break-word; }
    table.stack-sm td:last-child { border-bottom: 0; }
    table.stack-sm td::before { content: attr(data-label); flex: 0 0 38%; max-width: 40%; text-align: left; font-size: 12px; font-weight: 600; color: #666; text-transform: uppercase; letter-spacing: .02em; padding-top: 2px; }
    table.stack-sm td[data-label=""]::before { display: none; }
    table.stack-sm td[data-label=""] { justify-content: flex-end; }
    table.stack-sm td > * { text-align: right; }
    table.stack-sm td > .cell-value { flex: 1 1 auto; min-width: 0; text-align: right; }
    table.stack-sm td > .cell-value > * { text-align: right; }
    table.stack-sm td .btn { display: inline-flex; align-items: center; justify-content: center; align-self: flex-end; min-height: 40px; }
    table.stack-sm .project-ref, table.stack-sm .project-name, table.stack-sm .project-meta { text-align: right; }
    .board-wrap { border: 0; overflow: visible; }
}

/* tablet portrait: two cards per row */
@media (min-width: 641px) and (max-width: 900px) {
    table.stack-sm tbody { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
    table.stack-sm tr { margin: 0; }
}

/* ---- phones ------------------------------------------------------------------------------------------------ */
@media (max-width: 640px) {
    main .row { flex-direction: column; align-items: stretch; gap: 10px; }
    main .row > * { flex: 1 1 auto; min-width: 0; width: 100%; max-width: 100%; }
    main .row.wrap > .btn, main .row.wrap > a.btn { width: auto; }
    main form .btn, main .card > .btn { max-width: 100%; }
    .btn { padding: 10px 14px; font-size: 15px; }
    /* the main action of a form is a full-width button; secondary links keep their own width */
    main form button.btn[type=submit]:not(.secondary), main form input[type=submit].btn { width: 100%; }
    h1, h2, h3 { overflow-wrap: anywhere; }
    .card { border-radius: 10px; padding: 12px; }
    table { font-size: 14px; }

}

/* ---- small phones ------------------------------------------------------------------------------------------ */
@media (max-width: 400px) {
    main { padding: 10px 8px; }
    .card { padding: 10px; }
    h1 { font-size: 20px; }
    header.topbar a.brand { font-size: 16px; }
    table.stack-sm td::before { flex-basis: 34%; }
}

/* landscape phones: do not let a fixed drawer eat the whole height */
@media (max-height: 480px) and (orientation: landscape) {
    nav.mainmenu { max-height: 60vh; overflow-y: auto; }
}
