/* Phone layout. Nothing here applies above 768px, so the desktop stays as is.
   The !important flags are needed: most of the layout is set by inline styles,
   in index.html and in the markup app.js generates. */

/* #activityPill shares this z-index and comes later in the DOM: it showed on the login screen. */
#loginOverlay { z-index: 10000; }

/* iPhone save sheet (mobile.js). */
#mSaveSheet {
  display: none; position: fixed; inset: 0; z-index: 10001;
  align-items: flex-end; justify-content: center; padding: 12px;
  background: rgba(0,0,0,0.6);
}
#mSaveSheet.open { display: flex; }
#mSaveSheet .m-save-box {
  display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 420px; padding: 16px;
  background: var(--bg-card); border: 1px solid var(--border-card); border-radius: 16px;
}
#mSaveName { margin: 0; font-size: 13px; color: var(--text-muted); text-align: center; overflow-wrap: anywhere; }
#mSaveBtn, #mSaveClose { padding: 13px; border: 0; border-radius: 11px; font-size: 15px; font-weight: 700; cursor: pointer; }
#mSaveBtn { background: var(--accent); color: #fff; }
#mSaveBtn:disabled { background: #2a3347; color: var(--text-muted); }
#mSaveClose { background: transparent; color: var(--text-primary); }

/* The top bar and the backdrop only exist on phones. */
#mTopbar, #mBackdrop { display: none; }

/* Without a mouse, the card actions that only appear on hover are out of reach. */
@media (hover: none) {
  .img-actions, .vid-actions, .lab-actions { opacity: 1 !important; }
  .img-zoom, .jc-play, .jc-del, .jc-download,
  .apc-del, .apc-dl, .apc-star, .apc-retry,
  .img-result-dl, .model-rename-btn, .img-fav-star, .lib-del-btn { opacity: 1 !important; }
  .model-img-del { display: flex !important; }
  /* Always visible now, so they have to be hittable with a thumb. */
  .jc-del, .apc-del, .lib-del-btn, .model-img-del { width: 28px !important; height: 28px !important; }
}

@media (max-width: 768px) {
  /* On body, not :root: --m-topbar has to be computed where --m-quota changes. */
  body { --m-bar: 52px; --m-quota: 0px; --m-topbar: calc(var(--m-bar) + var(--m-quota)); --m-gutter: 14px; }
  /* app.js pins #quotaBar above everything once the quota is known. */
  body:has(#quotaBar) { --m-quota: 26px; }

  /* ── Shell: sidebar becomes a drawer, opened from a fixed top bar ───────── */
  body { padding-left: 0 !important; padding-top: var(--m-topbar) !important; }

  #mTopbar {
    display: flex; align-items: center; gap: 10px;
    position: fixed; top: var(--m-quota); left: 0; right: 0; z-index: 65;
    height: var(--m-bar); padding: 0 10px;
    background: var(--bg-sidebar); border-bottom: 1px solid var(--border-card);
  }
  #mMenuBtn {
    display: flex; align-items: center; justify-content: center; flex-shrink: 0;
    width: 40px; height: 40px; padding: 0; border: 0; border-radius: 10px;
    background: transparent; color: var(--text-primary); cursor: pointer;
  }
  #mTitle {
    flex: 1; min-width: 0; font-size: 15px; font-weight: 600; color: var(--text-primary);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }

  #sidebar {
    width: min(280px, 84vw); z-index: 8001;
    top: var(--m-quota); bottom: auto; height: calc(100dvh - var(--m-quota));
    transform: translateX(-100%); visibility: hidden;
    transition: transform 0.22s ease, visibility 0.22s;
  }
  body.m-nav-open #sidebar { transform: none; visibility: visible; box-shadow: 8px 0 40px rgba(0,0,0,0.55); }
  #sidebar .nav-link { font-size: 15px; padding: 0.65rem 0.75rem; }

  #mBackdrop {
    display: block; position: fixed; inset: 0; z-index: 8000;
    background: rgba(0,0,0,0.6); opacity: 0; pointer-events: none; transition: opacity 0.22s;
  }
  body.m-nav-open #mBackdrop { opacity: 1; pointer-events: auto; }

  /* ── Pages fill what the top bar leaves. dvh follows the phone's URL bar. ── */
  [id^="page-"] { height: calc(100dvh - var(--m-topbar)) !important; }
  [id^="page-"].p-8, [id^="page-"].p-6 { padding: var(--m-gutter) !important; }

  /* iOS zooms the whole page when focusing a field under 16px. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select, textarea { font-size: 16px !important; }

  /* Wide data tables scroll on their own rather than pushing the page. */
  [id^="page-"] table { display: block; max-width: 100%; overflow-x: auto; }

  /* ── Grids ──────────────────────────────────────────────────────────────── */
  #vidResultGrid, #labResultGrid, #imgResultGrid, #carGrid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 8px !important;
  }
  #vidResultGrid, #labResultGrid { padding: 10px 10px 90px !important; }
  #imgResultGrid { padding: 10px 10px 90px !important; }
  /* #jobList: renderMotionControlGrid() replaces .job-grid with grid-cols-4. */
  #jobList, #creationsList { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 8px !important; }
  .job-grid, .va-admin-grid, #afGrid, #spoofMlGrid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
  /* "No jobs" and "Load more" span 4: on two columns that would add two columns back. */
  [id^="page-"] .col-span-4 { grid-column: 1 / -1 !important; }

  /* Job cards are about 170px wide here: what sits side by side on a desktop stacks. */
  [data-job-id].card { padding: 10px !important; min-width: 0; }
  [data-job-id].card > .justify-between { flex-wrap: wrap; gap: 6px; }
  [data-job-id].card > .justify-between > :first-child { flex: 1 1 100%; }
  [data-job-id].card > .justify-between > .ml-2 { margin-left: 0; }
  [data-job-id].card .cr-verdict { flex: 0 0 auto; }
  [data-job-id].card .vbtn { padding: 4px 9px; font-size: 15px; }
  [data-job-id].card .cr-meta, [data-job-id].card .cr-models { flex-wrap: wrap; gap: 2px 6px; text-align: center; }
  [data-job-id].card .cr-foot { gap: 6px; }
  [data-job-id].card .cr-cost { flex-wrap: wrap; justify-content: center; gap: 4px 6px; }
  [data-job-id].card .cr-icbtn { width: 34px; height: 34px; }
  /* Download sits right under the video instead of at the bottom of a long card.
     Send to CRM stays where it is: it publishes, so it must not be one slip away. */
  [data-job-id].card > .aspect-\[9\/16\] { order: -2; }
  [data-job-id].card > .flex.items-center.gap-2.mt-1 { display: contents; }
  [data-job-id].card > .flex.items-center.gap-2.mt-1 > * { flex: none !important; }
  [data-job-id].card > .flex.items-center.gap-2.mt-1 > a[title="Download"] { order: -1; }
  /* Side-by-side panels stack. */
  [id^="page-"] [style*="grid-template-columns:1fr 1fr"],
  [id^="page-"] [style*="grid-template-columns:1fr 320px"] { grid-template-columns: minmax(0, 1fr) !important; }
  #page-clipping > * > .grid-cols-2,
  #page-dashboard .grid-cols-2:not(#dashKpis) { grid-template-columns: minmax(0, 1fr) !important; }

  .va-kanban-col { width: min(85vw, 300px); max-height: calc(100dvh - 130px - var(--m-topbar)); }

  /* ── Floating generation forms: desktop offsets them to clear the sidebar ── */
  #jobFormWrapper, #vidControlBar {
    left: 8px !important; right: 8px !important; bottom: 8px !important;
    width: auto !important; max-width: none !important; transform: none !important;
    max-height: calc(100dvh - var(--m-topbar) - 16px); overflow-y: auto;
  }
  #imgControlBar, #labControlBar { padding: 8px !important; }
  #imgControlBar { max-height: calc(100dvh - var(--m-topbar) - 16px); overflow-y: auto; }
  #jobFormWrapper > div, #vidControlBar > div,
  #imgControlBar > div, #labControlBar > div { padding: 12px !important; }
  #openJobFormBtn, #openVidFormBtn, #openLabBoxBtn, #openImgBoxBtn { right: 14px !important; bottom: 14px !important; }

  /* ── Modals ─────────────────────────────────────────────────────────────── */
  .jc-modal-overlay { padding: 8px; }
  #securityModal > div, #retryPromptModal > div {
    width: auto !important; max-width: calc(100vw - 24px) !important;
    max-height: calc(100dvh - 24px); overflow-y: auto;
  }
  #carModal { padding: 8px !important; }

  /* ── Docs: the section list moves above the text ────────────────────────── */
  #page-docs { flex-direction: column !important; }
  #page-docs > aside {
    width: 100% !important; max-height: 34%; padding: 10px 14px !important;
    border-right: 0 !important; border-bottom: 1px solid #2a2a33;
  }
  #page-docs > main { padding: 16px var(--m-gutter) !important; min-height: 0; }

  /* ── Toolbars too wide for a phone ──────────────────────────────────────── */
  /* Image: the grid is positioned under this 56px bar, so it scrolls rather than wraps. */
  #page-image > div:has(> #imgIdentityFilter) { overflow-x: auto; gap: 8px !important; padding: 8px 10px !important; }
  #imgIdentityFilter { min-width: 170px !important; flex-shrink: 0; }
  #imgFavTabs, #imgSelectBtn, #imgSelBar { flex-shrink: 0; }

  #page-lab > div:has(> #labClearFailedBtn) { flex-wrap: wrap; gap: 8px !important; padding: 10px !important; }
  #page-lab > div:has(> #labClearFailedBtn) > span { display: none; }
  #page-lab div:has(> #labModeQwen) { order: 1; width: 100%; flex-wrap: nowrap !important; overflow-x: auto; }
  .lab-mode-btn { flex-shrink: 0; white-space: nowrap; }
  #labClearFailedBtn { margin-left: auto; }
  #page-lab div:has(> #labGenerateBtn) { flex-wrap: wrap; }
  #labGenerateBtn { flex: 1 1 100%; min-width: 0; justify-content: center; white-space: normal; }

  #page-spoofing > div { padding: 16px var(--m-gutter) !important; }
  #page-spoofing div:has(> #spoofPresetSelect) { flex-wrap: wrap; width: 100%; }
  #spoofPresetSelect, #spoofPresetName { flex: 1 1 100%; width: 100% !important; }

  /* A flex field otherwise keeps its intrinsic width and pushes its button out. */
  [id^="page-"] .flex > input { min-width: 0; }
  [id^="page-"] .flex:has(> #newPresetScope) { flex-wrap: wrap; }
  #newPresetName { flex-basis: 100%; }

  /* Sits in the body flow outside any page: its padding alone shifted Spoofing down 40px. */
  #clipJobsList { padding-bottom: 0 !important; }

  #onboardingBanner { flex-wrap: wrap; padding: 10px 36px 10px 14px !important; }
}
