/* =============================================================================
   DEL NDT Portal — responsive layer (phones, tablets, iPad, touch screens)
   Loaded on every portal page by includes/sidebar.php, after the page's own styles.
   Desktop (1024 px and wider) is unchanged except for safe text wrapping.
   To roll back: remove the two lines that load responsive.css / responsive.js in includes/sidebar.php.

   Breakpoints
     ≥1920 large desktop · 1440–1919 desktop · 1024–1439 laptop   → existing desktop layout
     768–1023 tablet / iPad landscape                              → sidebar becomes a drawer
     600–767  tablet / iPad portrait                               → 2-column forms become 1 column
     ≤599     phones                                               → stacked cards, full-width buttons
   ============================================================================= */

/* ---------- all sizes: never let long text or images widen the page ---------- */
img, video, canvas, iframe { max-width: 100%; }
img { height: auto; }
td, th, .card, .kpi, .content p, .content li, .content dd { overflow-wrap: break-word; }
.content pre { white-space: pre-wrap; overflow-wrap: break-word; }
.rt-scroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.rt-scroll > table { min-width: 100%; }

/* visible keyboard focus (mouse clicks are not affected) */
a:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible, [tabindex]:focus-visible {
    outline: 2px solid var(--gold, #B8892B); outline-offset: 2px;
}

/* mobile menu button + backdrop (hidden on desktop) */
.rs-menu { display: none; }
.rs-backdrop { display: none; }

/* very large screens: keep lines readable */
@media (min-width: 1920px) {
    .content { padding: 28px 36px; }
}

/* ---------- touch screens of any size: comfortable targets, no hover-only actions ---------- */
@media (pointer: coarse) {
    .btn, button, .sidebar nav a, .tabs a { min-height: 40px; }
    .form-group input:not([type=checkbox]):not([type=radio]), .form-group select, .form-group textarea,
    .content input[type=text], .content input[type=search], .content input[type=email], .content input[type=tel],
    .content input[type=number], .content input[type=date], .content input[type=time], .content input[type=password], .content select {
        min-height: 42px;
    }
    input[type=checkbox], input[type=radio] { width: 20px; height: 20px; }
    tr.job-row:hover { background: inherit; }
    details > summary { padding-top: 4px; padding-bottom: 4px; }
}

/* =============================================================================
   Tablets / iPad landscape and smaller (≤1023 px): sidebar becomes a drawer
   ============================================================================= */
@media (max-width: 1023px) {
    .app { height: auto; min-height: 100vh; min-height: 100dvh; display: block; }
    .main { min-height: 100vh; min-height: 100dvh; }
    .content { overflow: visible; padding: 18px; }

    /* off-canvas sidebar */
    .sidebar {
        position: fixed; top: 0; bottom: 0; left: 0; z-index: 10001; width: min(300px, 86vw);
        transform: translateX(-102%); transition: transform .22s ease; overflow-y: auto; -webkit-overflow-scrolling: touch;
        box-shadow: 4px 0 24px rgba(0, 0, 0, .3); display: flex !important;
    }
    /* "collapsed sidebar" remembered from the old desktop layout (sb-collapsed) must not hide the menu on phones / iPad */
    html.sb-collapsed .sidebar, html .app > .sidebar { display: flex !important; }
    html.rs-nav-open .sidebar, html.sb-collapsed.rs-nav-open .sidebar { transform: translateX(0); }
    html.rs-nav-open, html.rs-nav-open body { overflow: hidden; }
    .sidebar nav a { padding: 12px 12px; font-size: 14px; }
    .sidebar > a { padding-top: 14px !important; }
    .rs-backdrop { position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 10000; background: rgba(8, 20, 34, .45); }
    html.rs-nav-open .rs-backdrop { display: block; }
    /* the desktop collapse buttons are replaced by the menu button */
    .sb-collapse, .sb-tab { display: none !important; }
    .sb-collapsed .main .topbar { padding-left: 64px !important; }

    .rs-menu {
        display: flex; position: absolute; top: 10px; left: 10px; z-index: 9999; width: 44px; height: 44px;
        align-items: center; justify-content: center; border: 0; border-radius: 10px; cursor: pointer;
        background: var(--navy, #0A1F33); color: #fff; box-shadow: 0 2px 10px rgba(0, 0, 0, .25);
    }
    .rs-menu svg { width: 22px; height: 22px; pointer-events: none; }
    .rs-menu, .rs-backdrop { touch-action: manipulation; -webkit-tap-highlight-color: transparent; cursor: pointer; }
    /* scrolled down the page: the button follows when scrolling back up (or while the menu is open) */
    html.rs-menu-float .rs-menu, html.rs-nav-open .rs-menu { position: fixed; }
    html.rs-menu-float:not(.rs-nav-open) .rs-menu { animation: rsDrop .18s ease; }
    @keyframes rsDrop { from { transform: translateY(-60px); } to { transform: none; } }
    html.rs-nav-open .rs-menu { left: calc(min(300px, 86vw) + 10px); }

    /* top bar: room for the menu button, wraps instead of overflowing */
    .topbar { height: auto; min-height: 64px; flex-wrap: wrap; padding: 10px 14px 10px 64px !important; gap: 8px 10px; position: relative; }
    .topbar .title { min-width: 0; flex: 1 1 auto; font-size: 16px; overflow-wrap: anywhere; }
    .topbar > div[style*="flex:1"], .topbar > div[style*="flex: 1"] { display: none; }
    .topbar input[type=text], .topbar input[type=search] { max-width: none; flex: 1 1 220px; }

    .grid-2 { grid-template-columns: 1fr; }
    /* grid columns may shrink below their widest content (long drop-down options, long names) */
    .form-grid > *, .grid-2 > *, .app .cols > *, .app .kvs > *,
    .app [style*="display:grid"] > *, .app [style*="display: grid"] > * { min-width: 0; }
    .app select, .app input:not([type=checkbox]):not([type=radio]), .app textarea { max-width: 100%; }
    /* equipment validity period (years / months / days) */
    .vp > div { min-width: 0; }
    .vp input { width: 100%; }
    .app .fgrid[style], .app .fgrid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
    .job-header .meta-grid { grid-template-columns: repeat(2, 1fr); }
    .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: thin; }
    .tabs a { white-space: nowrap; flex: none; }

    /* inline 3- and 4-column grids become 2 columns */
    .app [style*="grid-template-columns:1fr 1fr 1fr"], .app [style*="grid-template-columns: 1fr 1fr 1fr"],
    .app [style*="grid-template-columns:repeat(3"], .app [style*="grid-template-columns: repeat(3"],
    .app [style*="grid-template-columns:repeat(4"], .app [style*="grid-template-columns: repeat(4"] {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }

    /* pop-up windows fit the screen and scroll inside */
    .modal, .ut-modal > div { max-width: calc(100vw - 24px) !important; max-height: calc(100dvh - 24px); overflow-y: auto; }
}

/* =============================================================================
   Tablet / iPad portrait and smaller (≤767 px)
   ============================================================================= */
@media (max-width: 767px) {
    .form-grid { grid-template-columns: minmax(0, 1fr); }
    .form-grid > * { grid-column: auto !important; min-width: 0; }
    /* a long option in a drop-down (e.g. a job or client name) must not push its column off the screen */
    .app select, .app input:not([type=checkbox]):not([type=radio]), .app textarea { max-width: 100%; min-width: 0; }
    .app .form-group, .app .fg, .app [class*="field"] { min-width: 0; }
    /* rows of buttons / badges laid out inline (e.g. job header) wrap onto a second line */
    .app .content div[style*="display:flex"], .app .content div[style*="display: flex"], .job-header > div { flex-wrap: wrap; }
    .app .content div[style*="flex:1"]:empty, .app .content div[style*="flex: 1"]:empty { flex-basis: 100%; height: 0; }
    /* page-level tab rows scroll sideways instead of widening the page */
    .doc-tabs { display: flex; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .doc-tabs a { white-space: nowrap; flex: none; }
    /* page form grids used across Personnel / Suppliers / Bills / Settings pages */
    .app .fgrid, .app .two { grid-template-columns: minmax(0, 1fr) !important; }
    .app .fgrid > *, .app .two > *, .app .box { min-width: 0; }
    .app [style*="grid-template-columns"] { grid-template-columns: minmax(0, 1fr) !important; }
    .app [style*="grid-column"] { grid-column: auto !important; }
    .kpi { min-width: calc(50% - 8px); flex: 1 1 calc(50% - 8px); }
    .kpi-row { gap: 10px; }
    .kpi .value { font-size: 22px; }
    .card { padding: 16px; }
    .card[style*="max-width"] { max-width: none !important; }
    .form-actions { flex-wrap: wrap; }
    /* prevent iOS from zooming into small inputs */
    input:not([type=checkbox]):not([type=radio]), select, textarea { font-size: 16px !important; }
    .content form[style*="display:flex"], .content form[style*="display: flex"] { flex-wrap: wrap; }
    .content form[style*="display:flex"] > input, .content form[style*="display: flex"] > input,
    .content form[style*="display:flex"] > select, .content form[style*="display: flex"] > select {
        max-width: none !important; min-width: 0 !important; flex: 1 1 160px;
    }
}

/* =============================================================================
   Phones (≤599 px)
   ============================================================================= */
@media (max-width: 599px) {
    .content { padding: 12px; }
    /* long e-mail addresses, file names and report numbers break instead of widening the screen */
    td, th, .card, .kpi, .content p, .content li, .content dd, .content a, .content span { overflow-wrap: anywhere; }
    .pill, .tag, .chip, .ut-badge, .ut-status { overflow-wrap: normal; white-space: nowrap; }
    .btn { overflow-wrap: normal; }
    .topbar { padding: 8px 10px 8px 62px !important; }
    .topbar .title { font-size: 15px; flex-basis: 100%; }
    .topbar .btn { flex: 1 1 auto; justify-content: center; }
    .kpi { padding: 12px; }
    .job-header { padding: 16px; }
    .job-header .meta-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
    .form-actions .btn, .form-actions button { flex: 1 1 100%; justify-content: center; }
    .login-card { width: auto; margin: 16px; padding: 26px 20px; }
    th { padding: 8px 10px; }
    td { padding: 10px; }

    /* button groups wrap instead of running off the screen */
    td [style*="white-space:nowrap"], td [style*="white-space: nowrap"] { white-space: normal !important; }
    .modal, .ut-modal > div { max-width: calc(100vw - 16px) !important; width: calc(100vw - 16px) !important; }
}

@media screen and (max-width: 1023px) {
    /* list tables become one card per record, labelled with the table headings.
       responsive.js adds .rt-cards on phones, and on tablets only to tables too wide for the screen */
    table.rt-cards, table.rt-cards tbody, table.rt-cards tr, table.rt-cards td { display: block; width: 100% !important; }
    table.rt-cards thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
    table.rt-cards tr { border: 1px solid var(--line, #E4E6E0); border-radius: 8px; margin: 0 0 10px; background: var(--card, #fff); padding: 6px 0; }
    table.rt-cards td { border: 0; padding: 6px 12px; text-align: left !important; display: flex; gap: 12px; align-items: baseline; min-height: 0; }
    table.rt-cards td[data-label]::before {
        content: attr(data-label); flex: 0 0 38%; max-width: 38%; font-size: 10.5px; font-weight: 700; text-transform: uppercase;
        letter-spacing: .04em; color: var(--steel, #5B6B7A); font-family: 'Inter', sans-serif;
    }
    table.rt-cards td > * { max-width: 100%; }
    table.rt-cards td.rt-actions { flex-wrap: wrap; gap: 6px; white-space: normal !important; }
    table.rt-cards td.rt-actions::before { flex-basis: 100%; max-width: none; }
    table.rt-cards td.rt-empty { display: none; }
    table.rt-cards td[colspan] { display: block; }
    table.rt-cards td[colspan]::before { display: none; }
    .card[style*="padding:0"] table.rt-cards, .card[style*="padding: 0"] table.rt-cards { padding: 10px; }

}

/* report / document pages (A4 sheets): shrink the sheets to the screen instead of widening the page */
@media screen and (max-width: 1279px) {
    html.rs-doc .page, html.rs-doc .sheet { zoom: var(--rs-doc-zoom, 1); }
    html.rs-doc.rpdf-capturing .page, html.rs-doc.rpdf-capturing .sheet { zoom: 1; }
    html.rs-doc .toolbar { max-width: 100% !important; padding: 0 8px; }
}

@media print {
    .rs-menu, .rs-backdrop { display: none !important; }
}
