/*
 * Davix H2I PWA / mobile layer -- loaded by Studio and the Dashboard
 * after davix-h2i-design-system.css. Adds touch ergonomics, safe-area handling,
 * the phone bottom tab bar, bottom-sheet modals, and the snackbar/offline bar
 * used by assets/js/davix-h2i-pwa.js. Desktop layouts are unchanged: everything
 * size- or input-specific is gated behind a media query.
 */

:root {
  --pwa-nav-h: 58px;
  --pwa-nav-offset: 0px;          /* space the bottom tab bar takes, INCLUDING the home-bar safe area (px, set below) */
}
@media (max-width: 760px) {
  html.has-bottom-nav { --pwa-nav-offset: calc(var(--pwa-nav-h) + var(--sab)); }
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
a, button, .btn, .icon-btn, .side-link, .tab, .tool-item, .user-menu-item, .switch {
  touch-action: manipulation;
}

/* Installed-app feel: no rubber-band / accidental pull-to-refresh, and UI
   chrome is not text-selectable (content such as keys, tables, and inputs is). */
html.is-standalone,
html.is-standalone body { overscroll-behavior-y: none; }
html.is-standalone .btn,
html.is-standalone .icon-btn,
html.is-standalone .side-link,
html.is-standalone .tab,
html.is-standalone .tool-item,
html.is-standalone .pwa-tab,
html.is-standalone .topbar {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Fill the visible viewport (dvh tracks mobile browser chrome; 100vh does not). The notch / status bar / home
   bar are handled by each element through --sat / --sab / --sal / --sar (see davix-h2i-design-system.css). */
@supports (height: 100dvh) {
  .shell { min-height: calc(100dvh - var(--pwa-nav-offset)); }
}
html.has-bottom-nav body { padding-bottom: var(--pwa-nav-offset); }

/* ---------- Touch targets (>=44px) on coarse pointers ---------- */
@media (pointer: coarse) {
  .btn { min-height: 44px; padding-left: 14px; padding-right: 14px; }
  .btn-sm { min-height: 40px; }
  .icon-btn { width: 44px; height: 44px; }
  .side-link { min-height: 46px; }
  .tool-item, .tool-group-head { min-height: 44px; }
  .tab { min-height: 44px; }
  /* small switches, the round ? marks and link buttons keep their look but get a larger area to tap */
  .switch, .hint-btn, .btn-link { position: relative; }
  .switch::before, .hint-btn::before, .btn-link::before { content: ""; position: absolute; inset: -10px -6px; }
  .btn-link { min-height: 32px; }
  .user-menu-item { min-height: 44px; display: flex; align-items: center; }
  /* 16px stops iOS Safari zooming the page when a field gets focus. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]),
  select,
  textarea {
    font-size: 16px;
    min-height: 44px;
  }
  /* Keep the tab bar out of the way of the on-screen keyboard. */
  html.has-bottom-nav:has(input:focus, textarea:focus, select:focus) .pwa-bottom-nav { display: none; }
}

/* ---------- Bottom tab bar (phones) ---------- */
.pwa-bottom-nav {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 15; /* below the sidebar drawer (20/30) and its backdrop (19) */
  background: var(--ink);
  border-top: 1px solid var(--ink-border);
  /* The home bar sits below the tabs; side padding keeps the outer tabs clear of the phone's rounded corners. */
  padding: 0 max(10px, var(--sar)) var(--sab) max(10px, var(--sal));
  box-sizing: border-box;
}
/* iOS now and then leaves a strip between the bar and the bottom of the screen (its fixed-position box ends above the home bar):
   the bar paints that strip too, so there is never a gap in a different colour. */
.pwa-bottom-nav::after { content: ""; position: absolute; left: 0; right: 0; top: 100%; height: 160px; background: var(--ink); pointer-events: none; }
@media (max-width: 760px) {
  html.has-bottom-nav .pwa-bottom-nav { display: flex; }
}
.pwa-tab {
  flex: 1 1 0;
  min-width: 0;
  height: var(--pwa-nav-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0 2px;
  background: none;
  border: 0;
  position: relative;
  color: var(--ink-text-muted);
  font: inherit;
  font-size: 10.5px;
  font-weight: 600;
}
.pwa-tab svg { width: 22px; height: 22px; flex: none; fill: none; }
.pwa-tab span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pwa-tab.active { color: var(--accent); }
.pwa-tab.active::before {
  content: "";
  position: absolute;
  top: 0; left: 28%; right: 28%;
  height: 2px;
  border-radius: 0 0 2px 2px;
  background: var(--accent);
}
.pwa-tab:active { background: var(--ink-3); }

/* ---------- Snackbar + offline bar ---------- */
.pwa-snack {
  position: fixed;
  left: max(12px, var(--sal)); right: max(12px, var(--sar));
  bottom: calc(14px + var(--pwa-nav-offset));
  max-width: 480px;
  margin: 0 auto;
  z-index: 90;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 6px 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
  font-size: 12.5px;
}
.pwa-snack span { flex: 1; min-width: 0; }
.pwa-snack button { font: inherit; background: none; border: 0; color: var(--text-muted); min-height: 36px; padding: 0 10px; border-radius: 8px; }
.pwa-snack-action { color: var(--accent) !important; font-weight: 700 !important; }
.pwa-snack-close { font-size: 20px !important; line-height: 1; }

.pwa-offline-bar {
  position: fixed;
  top: var(--sat);
  left: 0; right: 0;
  z-index: 95;
  padding: 6px 12px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-3);
  color: var(--text);
  border-bottom: 2px solid var(--warn);
}

/* ---------- Phones: toasts and modals ---------- */
@media (max-width: 600px) {
  .toast-stack {
    left: max(12px, var(--sal)); right: max(12px, var(--sar));
    top: calc(var(--sat, 0px) + 64px); bottom: auto; max-width: none; align-items: stretch;
  }
  .toast { min-width: 0; }

  /* Bottom sheet instead of a centered dialog. */
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal {
    max-width: none;
    border-radius: 16px 16px 0 0;
    max-height: 90vh;
    max-height: 90dvh;
  }
  .modal-foot { padding-bottom: calc(14px + var(--sab)); }
}

/* Studio's topbar action collapses to an icon button on narrow phones so the
   title, run, notifications and account controls all fit on one row. (The result header's buttons, Export code included, keep their labels.) */
@media (max-width: 520px) {
  html[data-pwa-app="studio"] .topbar { gap: 8px; padding: var(--sat) max(12px, var(--sar)) 0 max(12px, var(--sal)); }
  html[data-pwa-app="studio"] #runPipelineBtn {
    font-size: 0;
    gap: 0;
    width: 44px; height: 44px; min-height: 0;
    padding: 0;
    justify-content: center;
    flex: none;
  }
  html[data-pwa-app="studio"] #runPipelineBtn svg { width: 18px; height: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  .pwa-tab, .pwa-snack { transition: none; }
}

/* ---------------------------------------------------------------------------------------------------------------------
   Installed app: no zoom (the pinch gesture itself is cancelled in davix-h2i-pwa.js; these rules cover double-tap and the
   focus-zoom iPhone applies to small text fields) -- a normal browser tab is not touched.
   --------------------------------------------------------------------------------------------------------------------- */
html.is-standalone,
html.is-standalone body { touch-action: pan-x pan-y; }
@media (pointer: coarse) {
  html.is-ios.is-standalone input,
  html.is-ios.is-standalone select,
  html.is-ios.is-standalone textarea { font-size: max(16px, 1em) !important; }
}

/* ---------------------------------------------------------------------------------------------------------------------
   Pull to refresh (davix-h2i-refresh.js): the transparent logo mark, wrapped by a gradient progress ring (the logo's own cyan > blue > magenta
   > pink), floats down from under the top bar. Pulling draws the ring and brings the mark in; past the mark it glows and pops (release to
   refresh); refreshing sends a bright comet round the mark; done turns the mark into a check.
   --------------------------------------------------------------------------------------------------------------------- */
.h2i-ptr {
  position: fixed; left: 0; right: 0; z-index: 5; top: 0;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  pointer-events: none; visibility: hidden; opacity: 0;
  transform: translate3d(0, var(--ptr-y, -64px), 0);
  will-change: transform, opacity;
}
.h2i-ptr.is-visible { opacity: calc(.2 + var(--ptr-p, 0) * .8); }
.h2i-ptr.h2i-ptr-anim { transition: transform .4s cubic-bezier(.2, .9, .25, 1.12), opacity .28s ease; }
.h2i-ptr-badge { position: relative; width: 56px; height: 56px; display: grid; place-items: center; }
.h2i-ptr-glow {
  position: absolute; inset: -14px; border-radius: 50%; pointer-events: none;
  background: radial-gradient(closest-side, rgba(214, 60, 255, .42), rgba(47, 91, 255, .22) 55%, transparent 72%);
  opacity: calc(var(--ptr-p, 0) * .55); transform: scale(calc(.7 + var(--ptr-p, 0) * .3));
  transition: opacity .25s ease, transform .3s cubic-bezier(.2, 1.2, .3, 1);
}
.h2i-ptr[data-phase="armed"] .h2i-ptr-glow,
.h2i-ptr[data-phase="refreshing"] .h2i-ptr-glow { opacity: 1; transform: scale(1.12); }
.h2i-ptr[data-phase="refreshing"] .h2i-ptr-glow { animation: h2i-ptr-glow 1.4s ease-in-out infinite; }
.h2i-ptr-ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); overflow: visible; }
.h2i-ptr-track { fill: none; stroke: rgba(255, 255, 255, .09); stroke-width: 2; }
.h2i-ptr-arc { fill: none; stroke-width: 3; stroke-linecap: round; filter: drop-shadow(0 0 5px rgba(214, 60, 255, .55)); }
.h2i-ptr-logo {
  position: relative; width: 29px; height: 30px; display: block; object-fit: contain;
  opacity: calc(.25 + var(--ptr-p, 0) * .75); transform: scale(calc(.62 + var(--ptr-p, 0) * .38)) rotate(calc((1 - var(--ptr-p, 0)) * -16deg));
  filter: drop-shadow(0 2px 7px rgba(0, 0, 0, .35));
  transition: transform .28s cubic-bezier(.2, 1.3, .3, 1), opacity .2s ease;
}
.h2i-ptr[data-phase="armed"] .h2i-ptr-logo { opacity: 1; transform: scale(1.14) rotate(0deg); }
.h2i-ptr-check { position: absolute; width: 34px; height: 34px; opacity: 0; transform: scale(.4); transition: transform .34s cubic-bezier(.2, 1.5, .4, 1), opacity .2s ease; }
.h2i-ptr-label { font-size: 11px; font-weight: 600; letter-spacing: .03em; color: var(--text-muted, #98a2b3); text-shadow: 0 1px 3px rgba(0, 0, 0, .45); }
/* refreshing: a comet circles the mark, the mark breathes */
.h2i-ptr.is-spinning .h2i-ptr-ring { animation: h2i-ptr-spin .9s linear infinite; }
.h2i-ptr.is-spinning .h2i-ptr-arc { stroke-dasharray: 46 200; stroke-dashoffset: 0 !important; }
.h2i-ptr.is-spinning .h2i-ptr-logo { opacity: 1; transform: scale(1); animation: h2i-ptr-breathe 1.2s ease-in-out infinite; }
/* done: the ring closes and the mark becomes a check */
.h2i-ptr.is-done .h2i-ptr-logo { opacity: 0; transform: scale(.5); animation: none; }
.h2i-ptr.is-done .h2i-ptr-check { opacity: 1; transform: scale(1); }
.h2i-ptr.is-done .h2i-ptr-arc { stroke-dasharray: 150.8 200; stroke-dashoffset: 0 !important; }
@keyframes h2i-ptr-spin { to { transform: rotate(270deg); } }
@keyframes h2i-ptr-breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(.88); } }
@keyframes h2i-ptr-glow { 0%, 100% { opacity: .75; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .h2i-ptr.h2i-ptr-anim { transition: opacity .2s ease; }
  .h2i-ptr-logo { transform: none; }
  .h2i-ptr.is-spinning .h2i-ptr-ring { animation: none; }
  .h2i-ptr.is-spinning .h2i-ptr-logo { animation: h2i-ptr-breathe 1.6s ease-in-out infinite; }
  .h2i-ptr[data-phase="refreshing"] .h2i-ptr-glow { animation: none; }
}

/* Title bar of the installed app on a computer (Window Controls Overlay): the page draws in it, so it keeps its own strip with a Refresh button
   next to the window buttons, and the page starts below it (the top inset is the strip's height, like a phone's notch). */
.pwa-titlebar { display: none; }
html.has-titlebar { --sat: var(--pwa-titlebar-h, 33px); }
html.has-titlebar .pwa-titlebar {
  display: block; position: fixed; z-index: 2000; top: 0; left: 0; right: 0; height: var(--pwa-titlebar-h, 33px);
  background: var(--ink, #0b0d10); border-bottom: 1px solid var(--ink-border, rgba(255,255,255,.08));
  -webkit-app-region: drag; app-region: drag; user-select: none; -webkit-user-select: none;
}
html.has-titlebar .pwa-titlebar[hidden] { display: none; }
.pwa-titlebar-in {
  position: absolute; top: 0; bottom: 0; left: env(titlebar-area-x, 0px); width: env(titlebar-area-width, 100%);
  display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 6px 0 12px; box-sizing: border-box;
}
.pwa-titlebar-in { gap: 8px; }
.pwa-titlebar-logo { width: 18px; height: 18px; flex: none; pointer-events: none; }
.pwa-titlebar-name { font: 700 12.5px/1 var(--font-ui, system-ui, sans-serif); color: var(--ink-text, #e8eaf0); white-space: nowrap; flex: none; }
.pwa-titlebar-sep { color: var(--ink-text, #e8eaf0); opacity: .35; font: 500 12.5px/1 var(--font-ui, system-ui, sans-serif); flex: none; }
.pwa-titlebar-sep[hidden] { display: none; }
.pwa-titlebar-section { font: 500 12.5px/1 var(--font-ui, system-ui, sans-serif); color: var(--ink-text, #e8eaf0); opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.pwa-titlebar-fill { flex: 1 1 auto; }
.pwa-titlebar-refresh {
  -webkit-app-region: no-drag; app-region: no-drag; flex: none; width: 34px; height: calc(var(--pwa-titlebar-h, 33px) - 6px); display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 6px; background: transparent; color: var(--ink-text, #e8eaf0); opacity: .8; cursor: pointer;
}
.pwa-titlebar-refresh:hover { background: rgba(255,255,255,.1); opacity: 1; }
.pwa-titlebar-refresh:active { background: rgba(255,255,255,.16); }
.pwa-titlebar-refresh svg { width: 15px; height: 15px; }
