/* Author: Steven Thefaine */
/* =====================================================================
   ESI Portal — shared mobile layer (pairs with /static/esi-mobile.js)

   Injected into every portal and plugin page by main.py (mobile_inject).
   EVERY rule lives inside the phone / small-tablet media query below, so
   desktop and landscape-tablet layouts (> 768px) are never touched.

   esi-mobile.js only tags elements with esi-m-* classes (and only while
   the viewport is phone-sized); this file decides how they look:
     .esi-m-bar / .esi-m-row / .esi-m-item   collapsible menu bar (☰)
     .esi-m-cards                            table shown as stacked cards
     .esi-m-scroll                           table scrolls sideways
     .esi-m-stack / .esi-m-stack2            multi-column grid -> 1 / 2 cols
     .esi-m-fwrap / .esi-m-fit               overflowing row wraps / fits

   Opt-outs for plugin authors:
     data-mobile="cards" | "scroll" | "off"  on a <table>
     class="esi-m-skip"                      on any container
   ===================================================================== */

@media (max-width: 768px) {

  /* ---- base ---------------------------------------------------------- */
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  html body { overflow-wrap: break-word; }
  html body :is(img, video, canvas, iframe, embed, object, svg):not(.esi-m-skip *) { max-width: 100%; }
  html body :is(img, video):not(.esi-m-skip *) { height: auto; }
  html body pre { max-width: 100%; overflow-x: auto; white-space: pre-wrap; }

  /* 16px inputs stop iOS Safari zooming in when a field is tapped */
  html body :is(input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=color]):not([type=hidden]), select, textarea) {
    font-size: 16px !important;
    max-width: 100%;
  }

  /* comfortable tap targets */
  html body :is(.btn, button, [role=button], input[type=submit], input[type=button]):not(.esi-m-skip *) {
    min-height: 36px;
    touch-action: manipulation;
  }

  /* ---- generic fixes applied by esi-mobile.js ------------------------ */
  html body .esi-m-fit { max-width: 100% !important; min-width: 0 !important; }
  html body .esi-m-fwrap { flex-wrap: wrap !important; row-gap: 8px; }
  html body .esi-m-fwrap > * { min-width: 0; }
  html body .esi-m-fwrap > .esi-m-full { flex: 1 1 100% !important; width: auto !important; max-width: 100% !important; }
  html body .esi-m-stack { grid-template-columns: minmax(0, 1fr) !important; }
  html body .esi-m-stack > * { grid-column: auto !important; min-width: 0; }
  html body .esi-m-stack2 { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  html body .esi-m-stack2 > * { grid-column: auto !important; min-width: 0; }
  html body .esi-m-colflex { flex-direction: column !important; align-items: stretch !important; }
  html body .esi-m-colflex > * { width: auto !important; max-width: 100% !important; flex-basis: auto !important; min-width: 0 !important; }
  html body .esi-m-hscroll {
    overflow-x: auto !important; flex-wrap: nowrap !important; white-space: nowrap;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  html body .esi-m-hscroll::-webkit-scrollbar { display: none; }
  html body .esi-m-hscroll > * { flex: 0 0 auto !important; }

  html body .esi-m-snap { scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch; scroll-padding-left: 12px; }
  html body .esi-m-snap > *, html body .esi-m-snap > * > * { scroll-snap-align: start; }

  /* ---- menu bar: brand + ☰, everything else in a drop-down panel ----- */
  html body .esi-m-bar { height: auto !important; min-height: 0 !important; }
  html body .esi-m-row {
    display: flex !important; flex-wrap: wrap !important; align-items: center !important;
    gap: 8px !important; padding: 8px 12px !important; height: auto !important; min-height: 52px;
    max-width: 100% !important; width: auto !important;
  }
  html body .esi-m-row > .esi-m-brand { flex: 1 1 auto !important; min-width: 0 !important; overflow: hidden; margin: 0 !important; }
  html body .esi-m-row > .esi-m-brand :is(h1, h2, .brand, .title, .header-title) { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  html body .esi-m-burger {
    display: inline-flex !important; align-items: center; justify-content: center;
    flex: 0 0 auto; order: 9; margin-left: auto;
    width: 40px; height: 40px; min-height: 40px; padding: 0 !important;
    background: rgba(255, 255, 255, .12) !important; color: inherit !important;
    border: 1px solid rgba(255, 255, 255, .35) !important; border-radius: 8px !important;
    font: 600 20px/1 system-ui, sans-serif !important; cursor: pointer; box-shadow: none !important;
  }
  html body .esi-m-bar.esi-m-light .esi-m-burger {
    background: var(--esi-chip, #eef2f7) !important; border-color: var(--esi-line, #dde3ea) !important;
  }
  html body .esi-m-row > .esi-m-item { display: none !important; }
  html body .esi-m-row.esi-m-open > .esi-m-item {
    display: flex !important; flex-direction: column !important; align-items: stretch !important;
    flex-wrap: nowrap !important; gap: 2px !important; order: 10;
    flex: 1 1 100% !important; width: 100% !important; max-width: 100% !important;
    margin: 0 !important; padding: 4px 0 !important; position: static !important;
    transform: none !important; height: auto !important; max-height: none !important; overflow: visible !important;
  }
  html body .esi-m-row.esi-m-open > .esi-m-item.esi-m-actions {
    flex-direction: row !important; flex-wrap: wrap !important; align-items: center !important; gap: 8px !important;
    flex: 0 1 auto !important; width: auto !important; padding: 6px 0 !important; margin: 0 !important;
  }
  html body .esi-m-row.esi-m-open > .esi-m-item:not(.esi-m-actions) :is(nav, ul, ol, .nav, .navbar-nav, .menu, .links, .nav-links, .topbar-nav) {
    display: flex !important; flex-direction: column !important; align-items: stretch !important;
    flex-wrap: nowrap !important; gap: 2px !important; width: 100% !important; margin: 0 !important; padding: 0 !important;
    position: static !important; height: auto !important; overflow: visible !important;
  }
  html body .esi-m-row.esi-m-open > .esi-m-item:not(.esi-m-actions) :is(a, .ddbtn, .nav-link, button:not(.esi-theme-btn)) {
    display: block !important; width: 100% !important; text-align: left !important;
    padding: 10px 12px !important; margin: 0 !important; white-space: normal !important; border-radius: 8px;
  }
  html body .esi-m-row.esi-m-open > .esi-m-item:not(.esi-m-actions) :is(li, .dd, .dropdown, .nav-item) {
    display: block !important; width: 100% !important; position: static !important; margin: 0 !important;
  }
  /* drop-down groups open inline (click the group to expand) */
  html body .esi-m-row.esi-m-open .esi-m-item :is(.ddmenu, .dropdown-menu, .submenu, .menu-dropdown) {
    position: static !important; inset: auto !important; transform: none !important; float: none !important;
    box-shadow: none !important; border: 0 !important; margin: 0 0 4px 12px !important; padding: 0 !important;
    min-width: 0 !important; width: auto !important; background: transparent !important;
  }
  html body .esi-m-row.esi-m-open .esi-m-item .ddmenu a { color: inherit !important; }
  html body .esi-m-row.esi-m-open .esi-m-item .dd:not(.open) .ddmenu { display: none !important; }
  html body .esi-m-row.esi-m-open .esi-m-item .dd.open .ddmenu { display: block !important; }
  html body .esi-m-row.esi-m-open .esi-m-item :is(select, input[type=search], input[type=text]) { width: 100% !important; }
  html body .esi-m-row.esi-m-open .esi-m-item.esi-m-actions * { flex-wrap: wrap !important; max-width: 100%; }

  html body .esi-m-title { font-weight: 600; color: inherit; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  /* ---- hidden sidebars come back as a slide-in drawer ----------------- */
  html body .esi-m-drawer {
    display: block !important; position: fixed !important; top: 0 !important; left: 0 !important; bottom: 0 !important;
    width: min(84vw, 300px) !important; max-width: none !important; height: auto !important; z-index: 10001 !important;
    overflow-y: auto !important; transform: translateX(-104%); transition: transform .2s ease;
    box-shadow: 0 10px 40px rgba(0, 0, 0, .35);
  }
  html.esi-m-drawer-open body .esi-m-drawer { transform: none; }
  html body .esi-m-shade { display: none; position: fixed; inset: 0; background: rgba(15, 20, 26, .45); z-index: 10000; }
  html.esi-m-drawer-open body .esi-m-shade { display: block; }
  html body .esi-m-drawer-btn {
    display: inline-flex !important; align-items: center; justify-content: center; flex: 0 0 auto;
    width: 40px; height: 40px; min-height: 40px; margin-right: auto; padding: 0 !important;
    background: var(--esi-chip, #eef2f7) !important; color: var(--esi-ink, #1d2733) !important;
    border: 1px solid var(--esi-line, #dde3ea) !important; border-radius: 8px !important;
    font: 600 20px/1 system-ui, sans-serif !important; cursor: pointer;
  }
  html body .esi-m-drawer-btn.esi-m-floating { position: fixed; top: 10px; left: 10px; z-index: 9998; box-shadow: var(--esi-shadow); }

  /* ---- tables as stacked cards ---------------------------------------- */
  html body table.esi-m-cards {
    display: block !important; width: 100% !important; min-width: 0 !important; max-width: 100% !important;
    border: 0 !important; background: transparent !important; box-shadow: none !important; table-layout: auto !important;
  }
  html body table.esi-m-cards > caption { display: block; text-align: left; }
  html body table.esi-m-cards > thead {
    position: absolute !important; width: 1px !important; height: 1px !important; overflow: hidden !important;
    clip: rect(0 0 0 0) !important; clip-path: inset(50%) !important; white-space: nowrap !important;
  }
  html body table.esi-m-cards > :is(tbody, tfoot) { display: block !important; width: 100% !important; }
  html body table.esi-m-cards > :is(tbody, tfoot) > tr,
  html body table.esi-m-cards > tr {
    display: block !important; width: auto !important; height: auto !important;
    margin: 0 0 10px !important; padding: 4px 12px !important;
    background: var(--esi-panel, #fff); border: 1px solid var(--esi-line, #dde3ea) !important; border-radius: 10px;
  }
  html body table.esi-m-cards > :is(tbody, tfoot) > tr > :is(td, th),
  html body table.esi-m-cards > tr > :is(td, th) {
    display: block !important; position: relative !important;
    width: auto !important; min-width: 0 !important; max-width: none !important; height: auto !important;
    padding: 7px 0 !important; border: 0 !important; border-bottom: 1px solid var(--esi-line, #e5e9ef) !important;
    text-align: left !important; white-space: normal !important;
    background: transparent !important; box-shadow: none !important; overflow-wrap: anywhere;
  }
  html body table.esi-m-cards > :is(tbody, tfoot) > tr > :is(td, th):last-child,
  html body table.esi-m-cards > tr > :is(td, th):last-child { border-bottom: 0 !important; }
  /* label column on the left, value flows normally on the right */
  html body table.esi-m-cards :is(td, th)[data-label] { padding-left: calc(36% + 12px) !important; min-height: 34px; }
  html body table.esi-m-cards :is(td, th)[data-label]::before {
    content: attr(data-label);
    position: absolute; left: 0; top: 7px; width: 36%;
    font-weight: 600; font-size: 12px; line-height: 1.5; text-align: left;
    color: var(--esi-muted, #5f6b7a); text-transform: none; letter-spacing: 0; white-space: normal;
  }
  /* long labels sit above the value instead of beside it */
  html body table.esi-m-cards :is(td, th)[data-label].esi-m-label-top { padding-left: 0 !important; }
  html body table.esi-m-cards :is(td, th)[data-label].esi-m-label-top::before { position: static; display: block; width: auto; margin-bottom: 2px; }
  html body table.esi-m-cards :is(td, th).esi-m-actions-cell { text-align: right !important; padding-left: 0 !important; }
  html body table.esi-m-cards :is(td, th).esi-m-actions-cell::before { display: none; }
  html body table.esi-m-cards :is(td, th):empty { display: none !important; }
  html body table.esi-m-cards :is(td, th) * { flex-wrap: wrap; }
  html body table.esi-m-cards :is(td, th) :is(.btn, button, a.button, .badge, .pill, .chip, .tag) { white-space: nowrap; flex-shrink: 0; }
  html body table.esi-m-cards :is(td, th) :is(input:not([type=checkbox]):not([type=radio]), select, textarea) { max-width: 100%; }
  html body table.esi-m-cards tr.esi-m-hidden-row { display: none !important; }
  /* clickable rows keep their hint */
  html body table.esi-m-cards > tbody > tr[onclick], html body table.esi-m-cards > tbody > tr[data-href] { cursor: pointer; }

  /* ---- tables that scroll sideways ------------------------------------ */
  html body table.esi-m-scroll {
    display: block !important; width: auto !important; max-width: 100% !important; min-width: 0 !important;
    overflow-x: auto !important; -webkit-overflow-scrolling: touch;
  }
  html body table.esi-m-scroll > :is(thead, tbody, tfoot) > tr > th { white-space: nowrap; }
  html body table.esi-m-scroll > :is(thead, tbody, tfoot) > tr > :is(td, th):not(.esi-m-narrow) { min-width: 7.5rem; }
  html body table.esi-m-scroll > :is(thead, tbody, tfoot) > tr > :is(td, th).esi-m-wide { min-width: 14rem; }
  html body table.esi-m-scroll > :is(thead, tbody, tfoot) > tr > :is(td, th):first-child.esi-m-pin {
    position: sticky; left: 0; z-index: 1; background: var(--esi-panel, #fff);
    box-shadow: 1px 0 0 var(--esi-line, #dde3ea);
  }
  html body :is(.table-responsive, .table-wrap, .table-container) { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* ---- dialogs ---------------------------------------------------------- */
  html body :is(.modal-dialog, .modal-content, .modal-box, .modal-card, .modal-panel, .modal > .box, .dialog, dialog) {
    max-width: calc(100vw - 16px) !important;
  }
  html body .modal-dialog { margin: 8px auto !important; }
  html body :is(.modal-content, .modal-box, .modal-card, .modal-panel, dialog) { max-height: calc(100vh - 16px); overflow-y: auto; }

  /* ---- floating ◐ / Portal Home / Logout cluster ------------------------ */
  html body .esi-chrome {
    right: 10px !important; bottom: calc(10px + var(--esi-m-bottom, 0px) + env(safe-area-inset-bottom, 0px)) !important;
    padding: 4px !important; gap: 4px !important;
  }
  html body .esi-chrome :is(a.portal-link, .btn-logout, .esi-theme-btn) { padding: 4px 8px !important; font-size: 12px !important; min-height: 32px; }

  /* the shared portal toggles: ◐ and friends never squeeze below 32px */
  html body .esi-theme-btn { min-height: 32px; }
}
