/* FRAGMENT — Phase 5.1: mobile shell. Everything here is scoped under
   html.m-narrow / html.m-touch (see js/mobile.js) so desktop is byte-for-byte
   unaffected — this file only ever narrows/restyles, never widens, what
   fragment.css already established. Loaded after fragment.css so these rules
   win the cascade.

   Layout trick: .m-bottomgroup and .m-overflow-group wrap the SAME buttons
   app.js/ui.js already wire up. `display: contents` on desktop makes the
   wrapper invisible to layout (children flow exactly as before); on mobile
   the wrapper itself becomes the fixed bar/panel. No duplicate IDs, no
   duplicate event listeners. */

.m-btn { display: none; }
.m-bottomgroup { display: contents; }
.m-overflow-group { display: contents; }

html.m-narrow {
  --m-bar: 56px;
}

/* ---------- Top bar ---------- */
html.m-narrow .topbar {
  height: calc(58px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 10px 0;
  gap: 10px;
}

html.m-narrow .crumbs { display: none; }

/* "PHASE 1" reads as a version label on desktop; on the phone shell it's just
   noise — swap it for a plain "APP" tag instead of removing it outright. */
html.m-narrow .phase-tag { font-size: 0; }
html.m-narrow .phase-tag::after { content: 'APP'; font-size: 10px; letter-spacing: 0.14em; }

html.m-narrow .m-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--panel-line);
  color: var(--text);
  border-radius: 8px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
html.m-narrow .m-btn:hover { border-color: var(--colA); }
html.m-narrow .m-more { margin-left: auto; }

/* Settings gear is reached via the hamburger on mobile — hide the duplicate */
html.m-narrow #btn-settings { display: none; }

/* ---------- Bottom app bar (Up / Add / sync status) ---------- */
html.m-narrow .m-bottomgroup {
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 8px;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--m-bar) + env(safe-area-inset-bottom));
  padding: 0 12px env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--bg) 90%, black);
  border-top: 1px solid var(--panel-line);
  z-index: 24;
}
html.m-narrow .m-bottomgroup #btn-up,
html.m-narrow .m-bottomgroup #btn-add {
  flex: 1;
  min-height: 44px;
  font-size: 13.5px;
}
html.m-narrow .m-bottomgroup .cloud-state,
html.m-narrow .m-bottomgroup .save-state {
  flex-shrink: 0;
}

/* ---------- Overflow panel (Share / Export) ---------- */
html.m-narrow .m-overflow-group {
  display: none;
  position: fixed;
  top: calc(58px + env(safe-area-inset-top) + 6px);
  right: 10px;
  flex-direction: column;
  gap: 6px;
  min-width: 220px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 12px;
  padding: 10px;
  z-index: 30;
  box-shadow: 0 14px 34px rgba(0,0,0,0.5);
}
html.m-narrow .m-overflow-group.open { display: flex; }
html.m-narrow .m-overflow-group .dropdown > button { width: 100%; text-align: left; }

/* ---------- Stage fills the space between the two bars ---------- */
html.m-narrow main {
  inset: calc(58px + env(safe-area-inset-top)) 0 calc(var(--m-bar) + env(safe-area-inset-bottom)) 0;
}
/* #stage already gets touch-action:none from fragment.css (js/zoom.js drives
   pinch/pan directly) — don't reintroduce 'manipulation' here, it would hand
   pinch back to the browser's native page-zoom instead of our camera. */
html.m-narrow { overscroll-behavior-y: contain; }

/* ---------- Level heading becomes a floating pill above the bottom bar ---------- */
html.m-narrow .level-head {
  top: auto;
  left: 50%;
  bottom: calc(var(--m-bar) + env(safe-area-inset-bottom) + 14px);
  transform: translateX(-50%);
  display: flex;
  align-items: baseline;
  gap: 8px;
  max-width: calc(100vw - 32px);
  background: color-mix(in srgb, var(--panel) 90%, transparent);
  border: 1px solid var(--panel-line);
  border-radius: 999px;
  padding: 9px 18px;
  box-shadow: 0 10px 26px rgba(0,0,0,0.4);
}
html.m-narrow .level-depth { margin: 0; font-size: 10px; white-space: nowrap; }
html.m-narrow .level-title {
  font-size: 15px;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 55vw;
}

/* ---------- Bigger touch targets wherever a coarse pointer is present,
   independent of viewport width (e.g. a touchscreen laptop) ---------- */
html.m-touch .actions button,
html.m-touch .dropdown > button,
html.m-touch .dropdown-menu button {
  min-height: 44px;
}

/* ---------- Phase 5.2: bottom action sheet (replaces the popover on m-touch) ---------- */
.m-sheet-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10,13,18,0.55);
  z-index: 45;
}
.m-sheet-backdrop.open { display: block; }
.m-sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 46;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-bottom: none;
  border-radius: 18px 18px 0 0;
  padding: 8px 10px calc(14px + env(safe-area-inset-bottom));
  box-shadow: 0 -14px 34px rgba(0,0,0,0.5);
  transform: translateY(100%);
  transition: transform .28s cubic-bezier(.2,.8,.3,1);
}
.m-sheet.open { transform: translateY(0); }
.m-sheet-handle {
  width: 100%;
  padding: 10px 0 12px;
  cursor: grab;
  touch-action: none;
}
.m-sheet-handle::before {
  content: '';
  display: block;
  width: 40px;
  height: 4px;
  margin: 0 auto;
  border-radius: 999px;
  background: var(--panel-line);
}
.m-sheet-items { display: flex; flex-direction: column; gap: 2px; }
.m-sheet-items button {
  background: none;
  border: none;
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  text-align: left;
  padding: 15px 14px;
  min-height: 52px;
  border-radius: 10px;
  cursor: pointer;
}
.m-sheet-items button:active { background: color-mix(in srgb, var(--colA) 14%, transparent); }
.m-sheet-items button.danger { color: #ff7b7b; }
.m-sheet-items button.danger:active { background: rgba(255,90,90,0.12); }

/* ---------- Phase 5.2: full-screen modals (edit / reader / auth / my maps) ----------
   A centered card is miserable with an on-screen keyboard in a phone-sized
   viewport — same markup, CSS-only becomes a full page. */
html.m-narrow .modal-overlay,
html.m-narrow .reader-overlay {
  align-items: stretch;
  justify-content: stretch;
}
html.m-narrow .modal-overlay .modal,
html.m-narrow .reader-overlay .reader-card {
  width: 100%;
  max-width: none;
  /* --vvh (js/mobile.js, visualViewport) tracks the space actually left above
     an on-screen keyboard on Android; falls back to the full viewport. */
  height: var(--vvh, 100dvh);
  max-height: var(--vvh, 100dvh);
  border: none;
  border-radius: 0;
  padding-top: calc(24px + env(safe-area-inset-top));
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}
