/* ==========================================================================
   Unifour Portal - phone & tablet layer (iOS Safari + Android Chrome).
   Loaded after portal.css by both base.html and admin_base.html. Everything
   here is additive: remove the <link> to fall back to the desktop-only look.
   ========================================================================== */

/* ---- Notch / home-indicator safe areas (needs viewport-fit=cover) ------ */
:root {
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}
html { -webkit-tap-highlight-color: rgba(2, 116, 179, .12); }
body { overscroll-behavior-y: none; }
.site-header, .mobile-topbar { padding-top: var(--safe-t); }
.portal-footer { padding-bottom: calc(36px + var(--safe-b)); }
.toast-stack { bottom: calc(22px + var(--safe-b)); }

/* ---- Touch devices: no sticky hover lifts, bigger hit areas ------------ */
@media (hover: none) {
  .btn:hover, .kpi-tile:hover, .heatmap .cell:hover { transform: none; }
  .kpi-tile:hover { box-shadow: var(--shadow-sm); }
  .chart-svg:hover .bar, .chart-svg:hover .hbar { opacity: 1; }
}
@media (pointer: coarse) {
  .btn { min-height: 44px; }
  .btn-sm { min-height: 38px; padding: 8px 14px; }
  .tabs button, .segmented a, .segmented button { min-height: 40px; }
  .switcher-item, .user-menu-pop a, .sidebar nav a { min-height: 44px; }
  input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }
  select, input:not([type="checkbox"]):not([type="radio"]):not([type="file"]) { min-height: 44px; }
}

/* ---- Phones ------------------------------------------------------------ */
@media (max-width: 720px) {
  /* iOS zooms the whole page when a focused field is under 16px. */
  input, select, textarea, .switcher-search { font-size: 16px !important; }

  /* Fixed backgrounds repaint on every scroll frame on iOS - drop it. */
  body { background-attachment: scroll; }

  main { padding-left: calc(12px + var(--safe-l)); padding-right: calc(12px + var(--safe-r)); }

  /* Header band: stack, and let the FY pills scroll sideways instead of
     wrapping into a ragged block of half-width buttons. */
  .header-band .top-row { flex-direction: column; align-items: stretch; }
  .co-head { align-items: flex-start; gap: 12px; }
  .co-logo { height: 44px; max-width: 96px; border-radius: 10px; }
  .header-band h1 { font-size: 1.2rem; overflow-wrap: anywhere; }
  .header-band .segmented,
  .segmented.scroll-x { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; width: 100%;
                        -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity; }
  .header-band .segmented::-webkit-scrollbar, .segmented.scroll-x::-webkit-scrollbar { display: none; }
  .header-band .segmented:not(.at-end) { -webkit-mask-image: linear-gradient(90deg, #000 85%, transparent);
                                                 mask-image: linear-gradient(90deg, #000 85%, transparent); }
  .header-band .segmented button, .header-band .segmented a { flex: 0 0 auto; scroll-snap-align: start; }
  .segmented a, .segmented button { flex: 1 0 auto; justify-content: center; white-space: nowrap; }
  .sync-line { gap: 8px 14px; font-size: .78rem; }
  .deadline-strip { grid-template-columns: 1fr; gap: 8px; }
  .deadline-card { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 14px; }
  .deadline-card .dl-date { margin: 0; font-size: .98rem; order: 2; text-align: right; }
  .deadline-card .dl-countdown { flex-basis: 100%; order: 3; margin-top: -4px; }
  .deadline-card { flex-wrap: wrap; }

  /* Tabs: sticky under the header, with a fade hinting there is more. */
  .tabs { position: sticky; top: calc(58px + var(--safe-t)); z-index: 20; border-radius: 12px;
          -webkit-mask-image: linear-gradient(90deg, #000 88%, transparent);
                  mask-image: linear-gradient(90deg, #000 88%, transparent);
          box-shadow: var(--shadow-sm); background: rgba(242, 245, 249, .94);
          backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); }
  .tabs.at-end { -webkit-mask-image: none; mask-image: none; }

  /* KPI tiles: two across, headline tile full width. */
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-tile { padding: 14px; border-radius: 14px; min-width: 0; }
  .kpi-tile.headline { grid-column: 1 / -1; }
  /* Headline + 3 tiles would leave the last one alone half-width. */
  .kpi-grid > .kpi-tile.headline:first-child ~ .kpi-tile:last-child:nth-child(even),
  .kpi-grid > .kpi-tile:not(.headline):first-child ~ .kpi-tile:last-child:nth-child(odd),
  .kpi-grid > .kpi-tile:only-child { grid-column: 1 / -1; }
  .kpi-tile.headline .kpi-value { font-size: 1.6rem; }
  .kpi-label { font-size: .64rem; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
  .kpi-value { overflow-wrap: anywhere; }

  /* Filing heatmap: fit all 12 periods on screen instead of scrolling. */
  .heatmap { min-width: 0; gap: 3px; grid-template-columns: 50px repeat(12, minmax(0, 1fr)); font-size: .6rem; }
  .heatmap .cell { border-radius: 4px; font-size: .62rem; }
  .heatmap .row-label { font-size: .64rem; }

  /* Side-by-side card grids collapse to one column. */
  .m-stack { grid-template-columns: 1fr !important; }

  /* Filter bars: fields share the row two-up, buttons stretch. */
  .filter-bar, .topbar-filter { padding: 12px; gap: 10px; }
  .filter-bar .field { flex: 1 1 140px; min-width: 0 !important; }
  .filter-bar .field .btn { width: 100%; }
  .filter-bar .field:first-child { flex-basis: 100%; }   /* company / FY picker: room for long names */

  /* Primary form submits span the width - easier thumb target. */
  form.inline .full .btn, .auth-form .btn, .drawer-body form > .btn[type="submit"] { width: 100%; }

  /* Company switcher opens as a full-width sheet. */
  .switcher-list { max-height: 55vh; }

  /* Drawers / modals become full-screen / bottom sheets. */
  .drawer, .drawer.wide { width: 100vw; height: 100dvh; border-radius: 0; padding-top: var(--safe-t); }
  .drawer-foot { padding-bottom: calc(16px + var(--safe-b)); }
  .modal-backdrop { align-items: flex-end; }
  .modal-box { width: 100%; max-width: none; border-radius: 20px 20px 0 0; max-height: 88dvh;
               padding: 22px 18px calc(22px + var(--safe-b)); }
  dialog.pdf-modal { width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; border-radius: 0; }
  .pdf-modal-head { padding-top: calc(12px + var(--safe-t)); }

  /* Sync Center rows: let the meta drop under the name. */
  .job-row { flex-wrap: wrap; padding: 14px; }
  .job-row .job-meta { text-align: left; flex-basis: 100%; padding-left: 50px; }

  /* Buttons in table/action cells wrap instead of forcing overflow. */
  .actions-row, td .btn + .btn, td .btn + a.btn { margin-top: 0; }
  .actions-row { flex-wrap: wrap; }
  td:last-child:has(.btn) { white-space: normal; }

  /* ---- Wide tables that stay tables: scroll inside the card, with the
     first column pinned so rows stay identifiable. */
  .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -16px; padding: 0 16px; }
  .table-scroll table { min-width: max-content; }
  .table-scroll th:first-child, .table-scroll td:first-child { position: sticky; left: 0; z-index: 1; background: var(--surface);
                                                               box-shadow: 1px 0 0 var(--border); }
  .table-scroll tr.totals-row td:first-child { background: var(--surface-2); }

  /* ---- Stacked tables: each row becomes a small card (added by JS to
     simple tables of 3-7 columns). Labels come from the header row. */
  table.m-cards, table.m-cards tbody, table.m-cards tr, table.m-cards td { display: block; width: 100%; }
  table.m-cards tr.m-head { display: none; }
  table.m-cards tr { border: 1px solid var(--border); border-radius: 14px; padding: 6px 12px; margin: 0 0 10px;
                     background: var(--surface); box-shadow: var(--shadow-xs); }
  table.m-cards tr:hover > td { background: transparent; }
  table.m-cards td { display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: right;
                     padding: 8px 0; border-bottom: 1px solid var(--border); min-width: 0; overflow-wrap: anywhere; }
  table.m-cards td:last-child { border-bottom: none; }
  table.m-cards td::before { content: attr(data-label); flex-shrink: 0; max-width: 45%; text-align: left; font-family: var(--font-sans);
                             font-size: .68rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3); }
  table.m-cards td[data-label=""]::before { display: none; }
  table.m-cards td[data-label=""] { justify-content: flex-end; flex-wrap: wrap; gap: 8px; }
  table.m-cards td[data-label=""] .btn { flex: 1 1 auto; }
  table.m-cards td:first-child { font-weight: 700; font-size: .92rem; }
  table.m-cards tr.totals-row { background: var(--surface-2); }
  .card:has(> table.m-cards), .card:has(> .table-scroll) { overflow-x: visible; }
}

/* ---- Very small phones (iPhone SE, older Androids) --------------------- */
@media (max-width: 380px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .site-header .brand-name { font-size: .84rem; }
  h1 { font-size: 1.2rem; }
}

/* ---- Admin shell ------------------------------------------------------- */
@media (max-width: 900px) {
  .sidebar { height: 100dvh; padding-top: var(--safe-t); padding-bottom: var(--safe-b); width: min(300px, 86vw); }
  .mobile-topbar { min-height: calc(56px + var(--safe-t)); padding-right: 64px; }
  .mobile-topbar button { min-width: 44px; min-height: 44px; justify-content: center; }
  .notif-bell { top: calc(8px + var(--safe-t)); right: calc(12px + var(--safe-r)); }
  .notif-pop { position: fixed; top: calc(62px + var(--safe-t)); left: 12px; right: 12px; width: auto; }
  .topbar { flex-wrap: wrap; padding: 10px 16px; }
  .topbar select { min-width: 0; }
  .page-pad { padding-bottom: calc(18px + var(--safe-b)); }
  .tabs { top: calc(56px + var(--safe-t)); }
  body.nav-locked { overflow: hidden; }
}

/* ==========================================================================
   Phone-first layer (v3): section bar, app mode, compact lists.
   ========================================================================== */
.m-only { display: none; }

/* ---- Admin section bar (replaces the hamburger on phones) ------------- */
.m-nav { display: none; }
@media (max-width: 900px) { .m-nav { display: flex; } }
body.in-app .m-nav { display: flex; }
.m-nav { position: sticky; top: calc(56px + var(--safe-t)); z-index: 29; align-items: center; gap: 6px;
         padding: 8px 12px; background: rgba(255, 255, 255, .95); backdrop-filter: blur(14px);
         -webkit-backdrop-filter: blur(14px); border-bottom: 1px solid var(--border); }
body:not(.in-app) .site-header ~ .m-nav { top: calc(58px + var(--safe-t)); }
.m-nav-row { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; flex: 1; min-width: 0;
             -webkit-mask-image: linear-gradient(90deg, #000 88%, transparent); mask-image: linear-gradient(90deg, #000 88%, transparent); }
.m-nav-row::-webkit-scrollbar { display: none; }
.m-nav a, .m-more summary { flex: 0 0 auto; padding: 8px 14px; border-radius: 999px; font-size: .84rem; font-weight: 600;
                            color: var(--text-2); text-decoration: none; background: var(--surface-2); white-space: nowrap;
                            list-style: none; cursor: pointer; line-height: 1.2; }
.m-more summary::-webkit-details-marker { display: none; }
.m-nav a.on, .m-more summary.on { background: var(--brand); color: #fff; }
.m-count { display: inline-block; background: var(--danger); color: #fff; border-radius: 999px; padding: 0 6px;
           font-size: .7rem; margin-left: 2px; }
.m-more { position: relative; flex-shrink: 0; }
.m-more-pop { position: absolute; right: 0; top: calc(100% + 8px); background: var(--surface); border: 1px solid var(--border);
              border-radius: 14px; box-shadow: var(--shadow-md); padding: 6px; min-width: 220px; z-index: 60; }
.m-more-pop a { display: block; background: none; border-radius: 8px; padding: 12px; font-weight: 500; color: var(--text-1); }
.m-more-pop a:active { background: var(--surface-2); }
@media (max-width: 900px) {
  body:has(.m-nav) .tabs { top: calc(108px + var(--safe-t)); }
  .mobile-topbar { border-bottom: none; }
}

/* ---- Inside the UnifourSync app: the app's top bar replaces the portal header */
body.in-app .mobile-topbar, body.in-app.is-admin .site-header, body.in-app .notif-bell,
body.in-app .portal-footer, body.in-app [data-back] { display: none !important; }
body.in-app .m-nav { top: 0; }
body.in-app .tabs { top: 52px !important; }
body.in-app .page-pad { padding-top: 14px; }
body.in-app .page-pad > h1:first-child { font-size: 1.3rem; margin-bottom: 10px; }

@media (max-width: 720px) {
  .m-only { display: block; }
  .d-only { display: none !important; }

  /* Overview: one compact row per client, details on tap */
  .m-list { margin: 0 -4px; }
  .m-row { border-bottom: 1px solid var(--border); }
  .m-row > summary { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 11px 4px; list-style: none; cursor: pointer; }
  .m-row > summary::-webkit-details-marker { display: none; }
  .m-row-main { flex: 1 1 100%; min-width: 0; }
  .m-row .nm { font-weight: 650; font-size: .92rem; line-height: 1.25; overflow: hidden; text-overflow: ellipsis;
               display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  .m-row .sub { font-size: .74rem; color: var(--text-3); margin-top: 2px; }
  .m-badges { display: flex; gap: 5px; flex-wrap: wrap; }
  .mb { font-size: .68rem; font-weight: 700; padding: 2px 7px; border-radius: 999px; white-space: nowrap; }
  .m-row[open] > summary { background: var(--surface-2); border-radius: 10px; }
  .m-row-body { padding: 6px 4px 14px; }
  .m-ob { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 0;
          border-bottom: 1px dashed var(--border); }
  .m-ob .t { font-size: .7rem; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--text-3); }
  .m-ob .p { font-size: .82rem; }
  .m-actions { display: flex; gap: 8px; margin-top: 10px; }
  .m-actions .btn { flex: 1; }

  /* Companies: name + login badge on one line, counts and actions under it */
  table.co-table, table.co-table > tbody { display: block; }
  table.co-table tr:first-child { display: none; }
  table.co-table tr { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 10px; padding: 12px 0;
                      border-bottom: 1px solid var(--border); }
  table.co-table td { display: block; padding: 0; border: none; min-width: 0; }
  table.co-table td:nth-child(1) { grid-column: 1; grid-row: 1; }
  table.co-table td:nth-child(2) { grid-column: 2; grid-row: 1; }
  table.co-table td:nth-child(3) { grid-column: 1 / -1; font-size: .76rem; }
  table.co-table td:nth-child(4) { grid-column: 1 / -1; }
  table.co-table .co-name { font-size: .92rem; overflow-wrap: anywhere; }
  table.co-table .co-sub { font-size: .72rem; }
  table.co-table .row-actions { display: flex; gap: 6px; flex-wrap: nowrap; }
  table.co-table .row-actions > .btn { flex: 1; min-height: 38px; padding: 6px 10px; }
  table.co-table .more-pop { right: 0; left: auto; }

  /* Long explanations: keep the first line, the rest on tap */
  .section-body > p.muted:first-child { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .section-body > p.muted:first-child:active, .section-body > p.muted:first-child.full { -webkit-line-clamp: unset; }
}

@media (max-width: 720px) {
  .batch-run .m { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
  details.section > summary { flex-wrap: wrap; row-gap: 6px; }
  table.co-table td:nth-child(3):empty, table.co-table td.muted:only-child { display: none; }
}
