/* ─────────────────────────────────────────────────────────────────────────────
   HABITAT PWA LAYER — mobile-native responsive shell + install/update/offline
   UI + safe-area + reduced-motion. Loaded AFTER app.css; additive only, so the
   desktop experience is untouched above the phone breakpoint.
   ──────────────────────────────────────────────────────────────────────────── */

/* ── reduced motion: quiet the avatar and all transitions (any viewport) ──── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .hearth .ring, .hearth .ring2, .hearth .core { animation: none !important; }
}

/* ── PWA chrome: install/update banners, iOS sheet, sync chip, toast ──────── */
.pwa-banner {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: max(88px, calc(env(safe-area-inset-bottom) + 88px));
  z-index: 9600; width: min(520px, calc(100vw - 24px));
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--chrome, #191f24); color: var(--chrome-ink, #e8e4da);
  border: 1px solid #ffffff1f; border-left: 3px solid var(--copper, #a05a28);
  border-radius: 12px; box-shadow: 0 16px 40px rgba(0,0,0,.34);
}
.pwa-banner.update { border-left-color: #7fae94; }
.pwa-banner .pwa-b-ic { font-size: 18px; color: #d9b98c; width: 20px; text-align: center; }
.pwa-banner .pwa-b-tx { display: flex; flex-direction: column; line-height: 1.25; flex: 1; }
.pwa-banner .pwa-b-tx b { font-size: 13.5px; }
.pwa-banner .pwa-b-tx span { font-size: 11.5px; color: var(--chrome-muted, #9aa4ab); }
.pwa-b-btn {
  border: 0; background: var(--copper, #a05a28); color: #fbf7f1; font: inherit; font-weight: 600;
  font-size: 12.5px; padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.pwa-b-btn.sm { padding: 5px 10px; font-size: 11.5px; }
.pwa-b-btn.ghost { background: transparent; border: 1px solid #ffffff33; color: var(--chrome-ink, #e8e4da); }
.pwa-banner.update .pwa-b-btn { background: #2e6e4e; }
.pwa-b-x { border: 0; background: transparent; color: var(--chrome-muted, #9aa4ab); font-size: 20px; line-height: 1; cursor: pointer; padding: 2px 6px; }

.pwa-sheet-wrap {
  position: fixed; inset: 0; z-index: 9700; background: rgba(20,16,10,.5);
  display: flex; align-items: flex-end; justify-content: center; backdrop-filter: blur(3px);
}
.pwa-sheet {
  width: min(560px, 100vw); background: var(--panel, #faf8f3); color: var(--ink, #232a30);
  border-radius: 18px 18px 0 0; padding: 20px 22px calc(22px + env(safe-area-inset-bottom));
  box-shadow: 0 -12px 44px rgba(0,0,0,.3); max-height: 82vh; overflow: auto;
}
@media (min-width: 560px) { .pwa-sheet-wrap { align-items: center; } .pwa-sheet { border-radius: 16px; } }
.pwa-sheet-h { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.pwa-sheet-h b { font-family: var(--font-display, "Segoe UI"); font-size: 17px; }
.pwa-steps { margin: 6px 0 0; padding-left: 20px; }
.pwa-steps li { margin: 9px 0; font-size: 14px; line-height: 1.4; }
.pwa-gl { display: inline-block; transform: translateY(1px); color: var(--copper-2, #7f4720); font-weight: 700; }
.pwa-note { margin-top: 14px; font-size: 12px; color: var(--ink-2, #55636d); background: var(--panel-2, #f3f0e8); border: 1px solid var(--line, #ddd6c8); border-radius: 8px; padding: 9px 11px; }
.pwa-muted { color: var(--ink-3, #8b95a0); } .pwa-sm { font-size: 11.5px; }
.pwa-sync-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.pwa-sync-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 11px; border: 1px solid var(--line, #ddd6c8); border-radius: 10px; background: #fffdf8; }
.pwa-sync-actions { display: flex; gap: 6px; }

.pwa-sync {
  position: fixed; left: 14px; bottom: max(90px, calc(env(safe-area-inset-bottom) + 90px));
  z-index: 9500; display: inline-flex; align-items: center; gap: 7px;
  background: var(--chrome, #191f24); color: var(--chrome-ink, #e8e4da);
  border: 1px solid #ffffff22; border-radius: 999px; padding: 7px 13px; font: inherit; font-size: 12px; cursor: pointer;
  box-shadow: 0 8px 26px rgba(0,0,0,.28);
}
.pwa-sync .pwa-sync-dot { width: 8px; height: 8px; border-radius: 50%; background: #d0a23c; }
.pwa-sync.syncing .pwa-sync-dot { background: #7fae94; animation: pwaPulse 1s ease-in-out infinite; }
.pwa-sync.has-conflict .pwa-sync-dot { background: #c65f5f; }
@keyframes pwaPulse { 50% { opacity: .4; } }

.pwa-toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: max(24px, calc(env(safe-area-inset-bottom) + 24px)); z-index: 9800;
  background: var(--chrome, #191f24); color: var(--chrome-ink, #e8e4da);
  padding: 11px 16px; border-radius: 11px; font-size: 12.5px; max-width: min(420px, 92vw);
  border-left: 3px solid #c8a069; box-shadow: 0 10px 30px rgba(0,0,0,.35);
}
.pwa-toast.warn { border-left-color: #c65f5f; }

/* ═══════════════════════ MOBILE (phone) — native app shell ═══════════════ */
@media (max-width: 720px) {
  :root {
    --pwa-top: calc(50px + env(safe-area-inset-top));
    --pwa-bottom: calc(58px + env(safe-area-inset-bottom));
  }

  /* prevent iOS auto-zoom on focus; larger tap targets */
  input[type=text], input[type=number], input[type=password], textarea, select { font-size: 16px !important; }
  .btn { min-height: 38px; }

  /* top bar: compact, notch-safe, no desktop search */
  #topbar {
    height: var(--pwa-top); padding-top: env(safe-area-inset-top);
    padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right));
    gap: 8px;
  }
  #topbar #searchbox, #topbar .ws { display: none; }
  #topbar .spacer { display: none; }
  #topbar #presChip, #topbar #ntfChip { display: none; } /* keep the phone bar to logo · approvals · user */
  #topbar .mark { flex: 1; font-size: 16px; }
  #topbar .tchip { padding: 7px 9px; }
  #topbar #userChip { max-width: 46vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  #cuibanner { top: var(--pwa-top); font-size: 10px; padding: 3px 8px; }

  /* windows become full-screen stacked "screens"; newest (highest z) covers */
  #desktop { inset: var(--pwa-top) 0 var(--pwa-bottom) 0; }
  .win {
    position: fixed !important;
    left: 0 !important; top: var(--pwa-top) !important; right: 0 !important;
    width: 100vw !important; height: calc(100vh - var(--pwa-top) - var(--pwa-bottom)) !important;
    min-width: 0 !important; min-height: 0 !important; border-radius: 0 !important; border: 0 !important;
    box-shadow: none !important;
  }
  .win > header { height: 52px; cursor: default; padding: 0 6px 0 12px; }
  .win > header .title { font-size: 16px; font-weight: 650; }
  .win > header .wbtn { order: 3; width: 38px; height: 38px; align-items: center; justify-content: center; }
  .win > .body { padding: 14px max(14px, env(safe-area-inset-left)) calc(16px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-right)); -webkit-overflow-scrolling: touch; }
  .win > .body.flush { padding: 0; }

  /* dock → bottom tab bar (horizontal scroll for the full app set) */
  #dock {
    left: 0; right: 0; bottom: 0; transform: none; width: 100vw;
    height: var(--pwa-bottom); border-radius: 0; border: 0; border-top: 1px solid #ffffff14;
    padding: 4px 4px env(safe-area-inset-bottom); gap: 0; justify-content: flex-start;
    overflow-x: auto; overflow-y: hidden; scrollbar-width: none; -webkit-overflow-scrolling: touch;
  }
  #dock::-webkit-scrollbar { display: none; }
  #dock .dockbtn { width: 68px; flex: 0 0 auto; padding: 6px 2px 4px; scroll-snap-align: start; }
  #dock .dockbtn span { font-size: 10px; }
  #dock .sep { display: none; }

  /* agent rail → full-screen sheet */
  #agentbar {
    top: var(--pwa-top); left: 0; right: 0; bottom: 0; width: 100vw; z-index: 6800;
    border-left: 0;
  }
  #agentbar .composer { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  #agentbar .composer input { font-size: 16px; }

  /* platform console stacks */
  #platform { inset: var(--pwa-top) 0 0 0; padding: 16px max(14px, env(safe-area-inset-left)) calc(20px + env(safe-area-inset-bottom)); }
  #platform .pgrid { grid-template-columns: 1fr; }
  #platform .pgrid .card { position: static; max-height: none; }

  /* login → single column, notch-safe */
  #login { grid-template-columns: 1fr; overflow: auto; }
  #login .brand {
    padding: calc(env(safe-area-inset-top) + 22px) 22px 20px;
    background-image: radial-gradient(520px 320px at 110% -20%, #2d3a41 0%, transparent 60%);
  }
  #login .brand h1 { font-size: 27px; margin: 18px 0 10px; }
  #login .brand .traits, #login .brand .foot { display: none; }
  #login .brand p.lede { font-size: 13px; }
  #login .gate { padding: 20px 20px calc(28px + env(safe-area-inset-bottom)); }

  /* modals/overlays fit the viewport */
  #palette { width: 94vw; top: 10vh; }
  #onboard .panel { width: 92vw; padding: 22px 20px; }
  #toasts { right: 12px; left: 12px; bottom: calc(var(--pwa-bottom) + 10px); align-items: stretch; }
  #toasts .toast { max-width: none; }
  .pwa-banner { bottom: calc(var(--pwa-bottom) + 12px); }
  .pwa-sync { bottom: calc(var(--pwa-bottom) + 12px); }

  /* home action grid → single column on small screens */
  .home-actions { grid-template-columns: 1fr; }
  /* platform override grid → single column */
  #platform [style*="grid-template-columns:1fr 1fr 1fr"] { grid-template-columns: 1fr !important; }
  /* kanban → horizontal scroll of columns */
  .kb { grid-template-columns: repeat(4, minmax(150px, 1fr)); overflow-x: auto; }
  /* login persona rows: bigger tap target */
  .persona { padding: 13px 16px; }
}

/* installed standalone: subtle top hairline so the status area reads native */
body.pwa-standalone #topbar { box-shadow: inset 0 -1px 0 #ffffff12; }

/* small-phone tightening */
@media (max-width: 380px) {
  #topbar #ntfChip { display: none; }
  .win > header .title { font-size: 15px; }
}
