/* =====================================================================
   Browns Digital Studio — Mobile Fixes (390px+)
   Fixes for canvas editor, layouts, and critical UI at small viewports
===================================================================== */

/* ========================================================================
   Canvas Editor Mobile Fix (P0)
   At < 900px: one pane at a time via bottom tabs, no horizontal overflow
======================================================================== */
@media (max-width: 900px) {
  /* Canvas body fills viewport properly */
  body.builder-immersive,
  body.builder-live {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  /* Builder pill toolbar fixes */
  #view-app .builder .b-pill {
    max-width: 100vw;
    overflow-x: visible;
    padding: 0 var(--space-2);
  }

  /* Save label: never truncate */
  #view-app .builder .b-pill .bp-save,
  #view-app .builder .b-pill .bp-label {
    min-width: max-content;
    white-space: nowrap;
  }

  /* Ensure toolbar scroll works properly */
  #view-app .builder .b-pill .bp-scroll {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  #view-app .builder .b-pill .bp-scroll::-webkit-scrollbar {
    display: none;
  }

  /* Canvas panes: show one at a time based on bottom tab selection */
  #view-app .builder .builder-panel-left,
  #view-app .builder .builder-panel-right {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 56px; /* room for bottom tabs */
    z-index: var(--z-modal);
    background: var(--bg-app);
    overflow-y: auto;
  }

  /* Show panels when their tab is active */
  body[data-builder-pane="library"] #view-app .builder .builder-panel-left,
  body[data-builder-pane="edit"] #view-app .builder .builder-panel-right {
    display: flex;
    flex-direction: column;
  }

  /* Canvas center view: always visible unless a panel is showing */
  #view-app .builder .canvas-wrap {
    display: block;
  }

  body[data-builder-pane="library"] #view-app .builder .canvas-wrap,
  body[data-builder-pane="edit"] #view-app .builder .canvas-wrap {
    display: none;
  }

  /* Bottom tab bar for canvas */
  .builder-bottom-tabs {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    background: var(--surface-raised);
    border-top: 1px solid var(--border);
    z-index: var(--z-nav);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .builder-bottom-tabs button {
    flex: 1;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-size: var(--text-2xs);
    font-weight: var(--weight-medium);
    cursor: pointer;
    transition: color var(--dur-fast) var(--ease-standard);
  }

  .builder-bottom-tabs button svg {
    width: 20px;
    height: 20px;
  }

  .builder-bottom-tabs button.active {
    color: var(--brand);
  }

  /* Hide desktop-only canvas controls on mobile */
  #view-app .builder .bp-zoom-desktop {
    display: none;
  }
}

/* ========================================================================
   App Shell Viewport Fix (P0)
   Body must fill exactly 100dvh, no bottom gap
======================================================================== */
body.app {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

#view-app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

#view-app .shell {
  flex: 1;
  min-height: 0;
  display: flex;
}

#view-app .main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

#view-app .page {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

/* ========================================================================
   Sidebar Mobile Fix (P0)
   Off-canvas sheet, closed by default, closes on navigation
======================================================================== */
@media (max-width: 768px) {
  #view-app .side {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--sidebar-w);
    transform: translateX(-100%);
    transition: transform var(--dur-layout) var(--ease-shell);
    z-index: var(--z-overlay);
    background: var(--bg-app);
    border-right: 1px solid var(--border);
  }

  /* Sidebar open state */
  body.sidebar-open #view-app .side {
    transform: translateX(0);
  }

  /* Overlay when sidebar is open */
  body.sidebar-open::before {
    content: '';
    position: fixed;
    inset: 0;
    background: var(--overlay);
    z-index: calc(var(--z-overlay) - 1);
    animation: fade-in var(--dur-fast) var(--ease-standard);
  }

  @keyframes fade-in {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  /* Mobile tab bar */
  .m-tabbar {
    display: flex;
  }

  /* Main content adjusts for tab bar */
  #view-app .page {
    padding-bottom: calc(56px + env(safe-area-inset-bottom, 0));
  }
}

@media (min-width: 769px) {
  /* Desktop: sidebar always visible */
  #view-app .side {
    position: relative;
    transform: none;
  }

  /* Hide mobile tab bar on desktop */
  .m-tabbar {
    display: none;
  }
}

/* ========================================================================
   Additional mobile touch targets and spacing
======================================================================== */
@media (max-width: 768px) {
  /* Minimum touch target size */
  button,
  a,
  input[type="checkbox"],
  input[type="radio"] {
    min-height: 44px;
    min-width: 44px;
  }

  /* Adjust form inputs for mobile */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  textarea,
  select {
    min-height: 44px;
    font-size: 16px; /* Prevents zoom on iOS */
  }
}

/* ========================================================================
   Horizontal overflow prevention
======================================================================== */
html,
body {
  overflow-x: hidden;
  max-width: 100vw;
}

* {
  box-sizing: border-box;
}

/* Prevent content from creating horizontal scroll */
.page,
.shell,
.main {
  max-width: 100%;
  overflow-x: hidden;
}
