/* ============================================================================
   Nexus · mobile.css
   ----------------------------------------------------------------------------
   All rules are media-gated to phones/tablets (<= 768px). A forced-mobile
   override (`body.is-mobile-forced`) mirrors the same rules so you can test
   the layout on desktop with `?forceMobile=1`.

   Owned components: drawers (left/right rails as off-canvas), bottom-bar
   (4-icon nav), bottom-sheet root, install pill, pull-to-refresh spinner,
   bubble action menu, composer safe-area + virtual-keyboard avoidance.
   ============================================================================ */

/* Tokens reused inside media block. */
:root {
  --nx-mobile-bar-h: 60px;
  --nx-drawer-w: min(86vw, 320px);
  --nx-safe-bot: env(safe-area-inset-bottom, 0px);
  --nx-kbd-inset: env(keyboard-inset-height, 0px);
}

/* PTR + bubble menu live outside media so coordinate-based JS positioning
   has stable z-index even when forced-mobile toggles. They do nothing if
   their elements aren't mounted. */
.nx-ptr {
  position: fixed;
  top: 0; left: 50%;
  transform: translate(-50%, -60px);
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--surface, #FBF6EB);
  box-shadow: 0 8px 24px -8px rgba(0,0,0,.25);
  display: grid; place-items: center;
  z-index: 9000;
  opacity: 0; pointer-events: none;
  transition: transform 200ms cubic-bezier(.2,.8,.2,1), opacity 160ms ease-out;
}
.nx-ptr-spin {
  width: 18px; height: 18px;
  border: 2px solid var(--rule, #DBCFB5);
  border-top-color: var(--primary, #5F7E63);
  border-radius: 50%;
  display: block;
}
.nx-ptr.refreshing .nx-ptr-spin { animation: nx-ptr-spin 720ms linear infinite; }

.nx-bubble-menu {
  position: fixed;
  z-index: 10000;
  background: var(--surface, #FBF6EB);
  border: 1px solid var(--rule, #DBCFB5);
  border-radius: 14px;
  box-shadow: 0 16px 40px -10px rgba(0,0,0,.32);
  padding: 6px;
  display: flex; flex-direction: column;
  min-width: 180px;
  opacity: 0;
  transform: scale(.96) translateY(4px);
  transition: opacity 140ms ease-out, transform 180ms cubic-bezier(.2,.8,.2,1);
}
.nx-bubble-menu.show { opacity: 1; transform: scale(1) translateY(0); }
.nx-bm-item {
  background: transparent; border: 0;
  padding: 10px 12px;
  text-align: left;
  font: inherit; color: inherit;
  border-radius: 10px;
  cursor: pointer;
  min-height: 40px;
}
.nx-bm-item:hover, .nx-bm-item:active { background: var(--surface-2, #F2EBDB); }

/* Sheet root (mounted regardless of media — only shown via .open class +
   transforms; CSS is the same on desktop in case the sheet is reused there). */
.nx-sheet-root {
  position: fixed; inset: 0;
  z-index: 9500;
  pointer-events: none;
}
.nx-sheet-root.hidden { display: none; }
.nx-sheet-backdrop {
  position: absolute; inset: 0;
  background: #000;
  opacity: 0;
  pointer-events: auto;
  transition: opacity 220ms ease-out;
}
.nx-sheet {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 92vh;
  background: var(--surface, #FBF6EB);
  border-radius: 22px 22px 0 0;
  box-shadow: 0 -16px 48px -16px rgba(0,0,0,.32);
  pointer-events: auto;
  transform: translateY(100%);
  display: flex; flex-direction: column;
  overflow: hidden;
  padding-bottom: var(--nx-safe-bot);
  will-change: transform;
}
.nx-sheet-handle {
  align-self: center;
  background: transparent; border: 0;
  padding: 10px 28px 4px;
  cursor: grab;
  touch-action: none;
}
.nx-sheet-handle span {
  display: block;
  width: 44px; height: 4px;
  border-radius: 2px;
  background: var(--rule, #DBCFB5);
}
.nx-sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 16px 12px;
  border-bottom: 1px solid var(--rule-2, #E8DFC8);
  touch-action: none;
}
.nx-sheet-title {
  font-family: var(--font-display, inherit);
  font-size: 18px; font-weight: 600;
  margin: 0;
}
.nx-sheet-close {
  background: transparent; border: 0;
  width: 36px; height: 36px;
  border-radius: 10px;
  display: grid; place-items: center;
  color: inherit; cursor: pointer;
}
.nx-sheet-close:active { background: var(--surface-2, #F2EBDB); }
.nx-sheet-body {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px;
}
.nx-sheet-list { display: flex; flex-direction: column; gap: 4px; }
.nx-sheet-item {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 2px 10px;
  align-items: start;
  background: transparent; border: 0;
  padding: 12px;
  border-radius: 12px;
  text-align: left; cursor: pointer;
  font: inherit; color: inherit;
  min-height: 56px;
}
.nx-sheet-item:active { background: var(--surface-2, #F2EBDB); }
.nx-sheet-item-name {
  grid-column: 1 / 2;
  font-family: var(--font-mono, ui-monospace, monospace);
  font-weight: 600;
  color: var(--primary-deep, #44604A);
}
.nx-sheet-item-desc {
  grid-column: 2 / 3; grid-row: 1 / 3;
  align-self: center;
  color: var(--ink-2, #4D453B);
  font-size: 14px;
}
.nx-sheet-empty {
  padding: 20px;
  text-align: center;
  color: var(--ink-3, #807565);
}

/* Install pill — visible regardless of media (the JS only mounts on mobile),
   but layout is sized for thumb reach. */
.nx-install-pill {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(var(--nx-mobile-bar-h, 60px) + 12px + var(--nx-safe-bot));
  z-index: 8500;
  background: var(--surface, #FBF6EB);
  border: 1px solid var(--rule, #DBCFB5);
  border-radius: 16px;
  box-shadow: 0 12px 32px -8px rgba(0,0,0,.2);
  padding: 12px 14px;
  display: flex; align-items: center; gap: 12px;
  transform: translateY(140%);
  opacity: 0;
  transition: transform 320ms cubic-bezier(.2,.8,.2,1), opacity 220ms ease-out;
}
.nx-install-pill.show { transform: translateY(0); opacity: 1; }
.nx-install-ico {
  flex: 0 0 32px;
  width: 32px; height: 32px;
  border-radius: 10px;
  background: var(--primary-soft, rgba(95,126,99,.10));
  color: var(--primary-deep, #44604A);
  display: grid; place-items: center;
}
.nx-install-text { flex: 1 1 auto; line-height: 1.25; }
.nx-install-text b { display: block; font-size: 14px; }
.nx-install-text small { font-size: 12px; color: var(--ink-3, #807565); }
.nx-install-go {
  flex: 0 0 auto;
  background: var(--primary, #5F7E63); color: #fff;
  border: 0; border-radius: 999px;
  padding: 8px 14px; font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer; min-height: 36px;
}
.nx-install-x {
  flex: 0 0 auto;
  background: transparent; border: 0; color: var(--ink-3, #807565);
  width: 28px; height: 28px;
  border-radius: 8px;
  display: grid; place-items: center; cursor: pointer;
}

/* iOS install sheet inner. */
.nx-ios-install { padding: 8px 4px 4px; }
.nx-ios-install-lead { margin: 0 0 12px; color: var(--ink-2, #4D453B); }
.nx-ios-install-steps { margin: 0; padding-left: 22px; line-height: 1.55; }
.nx-ios-install-steps li { margin: 6px 0; }
.nx-ios-share {
  display: inline-flex; vertical-align: -3px;
  margin: 0 2px;
  width: 22px; height: 22px;
  color: var(--primary-deep, #44604A);
}
.nx-ios-install-arrow {
  margin-top: 14px;
  display: flex; justify-content: center;
  color: var(--accent-deep, #A36D43);
  animation: nx-bounce 1400ms ease-in-out infinite;
}

/* Drawer backdrop is shared across both rails. */
.nx-mobile-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.36);
  opacity: 0; pointer-events: none;
  z-index: 7500;
  transition: opacity 220ms ease-out;
}
.nx-mobile-backdrop.show { opacity: 1; pointer-events: auto; }

/* ----------------------------------------------------------------------------
   Mobile-only layout.
   ---------------------------------------------------------------------------- */
@media (max-width: 768px) {
  body.is-mobile {
    /* Avoid iOS auto-zoom on input focus. */
    -webkit-text-size-adjust: 100%;
  }

  /* Hide desktop rails (new layout) and legacy sidebar — present them as
     off-canvas drawers via transform. */
  body.is-mobile #nx-rail-left,
  body.is-mobile #nx-rail-right,
  body.is-mobile #sidebar {
    position: fixed !important;
    top: 0; bottom: 0;
    width: var(--nx-drawer-w);
    max-width: 92vw;
    z-index: 8000;
    background: var(--surface, #FBF6EB);
    box-shadow: 0 16px 48px -16px rgba(0,0,0,.32);
    transition: transform 280ms cubic-bezier(.2,.8,.2,1);
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(var(--nx-safe-bot) + 12px);
    will-change: transform;
  }
  body.is-mobile #nx-rail-left,
  body.is-mobile #sidebar {
    left: 0;
    transform: translateX(-100%);
    border-right: 1px solid var(--rule, #DBCFB5);
  }
  body.is-mobile #nx-rail-right {
    right: 0;
    transform: translateX(100%);
    border-left: 1px solid var(--rule, #DBCFB5);
  }
  body.is-mobile #nx-rail-left.nx-drawer-open,
  body.is-mobile #sidebar.nx-drawer-open,
  body.is-mobile #nx-rail-right.nx-drawer-open {
    transform: translateX(0);
  }

  /* Main / chat area takes the whole viewport on mobile. */
  body.is-mobile #nx-app,
  body.is-mobile #app,
  body.is-mobile .app {
    grid-template-columns: 1fr !important;
    display: block;
  }
  body.is-mobile .main {
    padding-bottom: calc(var(--nx-mobile-bar-h) + var(--nx-safe-bot) + 8px);
  }

  /* Compose: full-width sticky bottom, with virtual-keyboard avoidance and
     safe-area-inset-bottom padding. */
  body.is-mobile #composer,
  body.is-mobile .composer {
    position: sticky;
    bottom: calc(var(--nx-mobile-bar-h) + var(--nx-kbd-inset) + var(--nx-safe-bot));
    z-index: 200;
    background: linear-gradient(180deg, transparent 0, var(--bg, #F4EEE2) 30%);
    padding-bottom: 8px;
  }
  body.is-mobile #composer textarea,
  body.is-mobile #input,
  body.is-mobile #nx-input,
  body.is-mobile textarea,
  body.is-mobile input[type="text"],
  body.is-mobile input[type="search"] {
    /* >=16px so iOS Safari doesn't auto-zoom on focus. */
    font-size: 16px;
  }

  /* Bigger tap targets for buttons and tool toggles. */
  body.is-mobile button,
  body.is-mobile .icon-btn,
  body.is-mobile .tool-toggle,
  body.is-mobile .btn {
    min-height: 44px;
    min-width: 44px;
  }

  /* Bottom mobile bar — frosted glass, 4 icons. */
  body.is-mobile #nx-mobile-bar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    height: calc(var(--nx-mobile-bar-h) + var(--nx-safe-bot));
    padding-bottom: var(--nx-safe-bot);
    z-index: 8800;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    align-items: stretch;
    background: color-mix(in srgb, var(--surface, #FBF6EB) 78%, transparent);
    -webkit-backdrop-filter: saturate(160%) blur(18px);
            backdrop-filter: saturate(160%) blur(18px);
    border-top: 1px solid var(--rule, #DBCFB5);
  }
  body.is-mobile .nx-mbar-btn {
    background: transparent; border: 0;
    color: var(--ink-2, #4D453B);
    display: grid;
    grid-template-rows: auto auto;
    place-items: center;
    gap: 2px;
    font: inherit; font-size: 11px;
    cursor: pointer;
    padding: 6px 4px;
    min-height: var(--nx-mobile-bar-h);
  }
  body.is-mobile .nx-mbar-btn svg { color: currentColor; }
  body.is-mobile .nx-mbar-btn[aria-pressed="true"],
  body.is-mobile .nx-mbar-btn:active {
    color: var(--primary-deep, #44604A);
  }

  /* While a drawer is open, dim main content slightly via backdrop only —
     keep main interactive enough to reveal feedback if user taps it. */

  /* Hide some desktop-only chrome on phones to declutter. */
  body.is-mobile .topbar-title .pillar,
  body.is-mobile .topbar-title .tag,
  body.is-mobile .privacy-badge {
    display: none !important;
  }

  /* Reduce widget density: only LCD strip + Nixie shown by default;
     other widgets hidden inside the chat area (still accessible inside
     the right drawer). */
  body.is-mobile .main #nx-widget-vfd,
  body.is-mobile .main #nx-widget-crt,
  body.is-mobile .main #nx-widget-scope,
  body.is-mobile .main #nx-widget-synth,
  body.is-mobile .main #nx-widget-ticker {
    display: none !important;
  }

  /* Bottom-sheet snap height fits viewport on mobile. */
  body.is-mobile .nx-sheet { height: 92vh; }

  /* Messages get extra bottom slack so the last bubble isn't tucked under
     the composer. */
  body.is-mobile #messages,
  body.is-mobile .messages {
    padding-bottom: 16px;
  }

  /* Bubble swipe affordance — small bg behind that fades in as user drags. */
  body.is-mobile .message,
  body.is-mobile .nx-msg {
    will-change: transform;
    touch-action: pan-y;
  }
}

/* Forced-mobile mirror (?forceMobile=1) — duplicates the @media block via the
   class so desktop testing renders mobile chrome regardless of viewport. */
body.is-mobile.is-mobile-forced #nx-rail-left,
body.is-mobile.is-mobile-forced #nx-rail-right,
body.is-mobile.is-mobile-forced #sidebar {
  position: fixed !important;
  top: 0; bottom: 0;
  width: var(--nx-drawer-w);
  max-width: 92vw;
  z-index: 8000;
  background: var(--surface, #FBF6EB);
  box-shadow: 0 16px 48px -16px rgba(0,0,0,.32);
  transition: transform 280ms cubic-bezier(.2,.8,.2,1);
}
body.is-mobile.is-mobile-forced #nx-rail-left,
body.is-mobile.is-mobile-forced #sidebar {
  left: 0; transform: translateX(-100%);
}
body.is-mobile.is-mobile-forced #nx-rail-right {
  right: 0; transform: translateX(100%);
}
body.is-mobile.is-mobile-forced #nx-rail-left.nx-drawer-open,
body.is-mobile.is-mobile-forced #sidebar.nx-drawer-open,
body.is-mobile.is-mobile-forced #nx-rail-right.nx-drawer-open {
  transform: translateX(0);
}
body.is-mobile.is-mobile-forced #nx-mobile-bar {
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(var(--nx-mobile-bar-h) + var(--nx-safe-bot));
  padding-bottom: var(--nx-safe-bot);
  z-index: 8800;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--surface, #FBF6EB) 78%, transparent);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
          backdrop-filter: saturate(160%) blur(18px);
  border-top: 1px solid var(--rule, #DBCFB5);
}
