/* ============================================================
 * layout.css — App shell grid and breakpoint behavior
 *
 * Actual scaffold built by app.js → buildScaffold():
 *
 *   <div id="app">                                ← #app (flex column, 100dvh)
 *     <div class="app-sidebar"></div>             ← Sidebar mount slot
 *     <main class="app-main">                     ← Content + composer column
 *       <div class="app-content"></div>           ← Landing/Chat/Spaces/...
 *       <div class="app-composer"></div>          ← Composer (chat view only)
 *     </main>
 *   </div>
 *
 * - Height: 100dvh with 100vh fallback (Req. 12.10).
 * - Breakpoints: ≤480 mobile, 481–1024 tablet, >1024 desktop (Req. 12.1).
 * - Mobile sidebar is fixed-position with backdrop (Req. 2.10, 2.11, 12.2).
 * - Composer ≤480: full width with ≤8px padding (Req. 12.3).
 * - Profile tabs ≤480: horizontal scroll-snap (Req. 12.8).
 * - Model_Selector ≤768: fullscreen (Req. 12.7).
 *
 * Uses only variables defined in tokens.css.
 * Validates: Requirements 11.7, 12.1–12.10, 13.6
 * ============================================================ */

/* ─── Root mount ─────────────────────────────────────────────── */
#app {
  height: 100vh;
  height: 100dvh;
  width: 100%;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: 100%;
  background: var(--bg-0);
  color: var(--text-1);
  overflow: hidden;
}

/* ─── Sidebar slot (desktop default visible) ─────────────────── */
.app-sidebar {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  height: 100%;
  background: var(--bg-1);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  z-index: var(--z-sticky);
}

/* The sidebar component itself fills the slot. */
.app-sidebar > .sidebar {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* ─── Main column ────────────────────────────────────────────── */
.app-main {
  grid-column: 2;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  background: var(--bg-0);
}

.app-content {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
}

.app-content > * {
  /* Each view fills the content area unless it manages its own scroll. */
  flex: 1 1 auto;
  min-height: 0;
}

.app-composer {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: var(--bg-1);
  padding: var(--space-3) var(--space-4);
}

.app-composer[hidden] {
  display: none !important;
}

/* ─── Burger trigger (only visible on tablet/mobile) ─────────────────── */
.sidebar-burger {
  display: none;
  position: fixed;
  top: var(--space-3);
  left: var(--space-3);
  z-index: calc(var(--z-overlay) + 1);
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text-1);
  font-size: 18px;
  line-height: 1;
  align-items: center;
  justify-content: center;
}

/* ─── Sidebar backdrop (for the mobile/tablet overlay state) ────────────
 * The sidebar component renders <div class="sidebar-backdrop" hidden>;
 * the toggle handler removes [hidden] when opening on a small viewport.
 * ────────────────────────────────────────────────────────────────────── */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: var(--z-overlay);
  pointer-events: auto;
}

/* ─── Tablet & Mobile (≤1024px) ────────────────────────────────────────
 * Sidebar collapses to a slide-in drawer; main column gets full width.
 * Burger appears in the top-left.
 * Requirements 2.10, 2.11, 12.2.
 * ────────────────────────────────────────────────────────────────────── */
@media (max-width: 1024px) {
  #app {
    grid-template-columns: minmax(0, 1fr);
  }

  .app-sidebar {
    grid-column: 1;
    position: fixed;
    inset: 0 auto 0 0;
    width: min(var(--sidebar-w), 88vw);
    z-index: calc(var(--z-overlay) + 2);
    transform: translateX(-100%);
    transition: transform var(--dur) var(--ease);
    box-shadow: var(--shadow-3);
  }

  .app-main {
    grid-column: 1;
  }

  /* `is-open` is toggled by the sidebar component on the .sidebar element
   * (a child of .app-sidebar). We slide the slot using :has(). All
   * evergreen browsers (Chrome 105+, Safari 15.4+, Firefox 121+) support
   * :has(); on older engines the sidebar simply remains in its CSS
   * default (visible, courtesy of grid layout) which is acceptable. */
  .app-sidebar:has(.sidebar.is-open) {
    transform: translateX(0);
  }

  .sidebar-burger {
    display: inline-flex;
  }
}

/* ─── Mobile (≤480) ────────────────────────────────────────────────────
 * Composer takes the full width with ≤8px lateral padding (Req. 12.3).
 * ────────────────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .app-composer {
    padding-left: var(--space-2);
    padding-right: var(--space-2);
    padding-top: var(--space-2);
    padding-bottom: var(--space-2);
  }

  .composer {
    width: 100%;
    max-width: 100%;
    border-radius: var(--radius-md);
  }
}

/* ─── Profile tabs horizontal scroll on ≤480 (Req. 12.8) ───────────── */
@media (max-width: 480px) {
  .profile-modal__tabs {
    overflow-x: auto;
    overflow-y: hidden;
    flex-wrap: nowrap !important;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .profile-modal__tab {
    flex: 0 0 auto;
    scroll-snap-align: start;
    white-space: nowrap;
  }
}

/* ─── Model_Selector fullscreen on ≤768 (Req. 12.7) ──────────────────── */
@media (max-width: 768px) {
  .modal-dialog[data-modal-id="model-selector"],
  .modal-dialog.model-selector-dialog {
    inset: 0;
    width: 100vw;
    width: 100dvw;
    height: 100vh;
    height: 100dvh;
    max-width: none;
    max-height: none;
    border-radius: 0;
    margin: 0;
    transform: none;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    position: fixed;
  }

  .modal-dialog[data-modal-id="model-selector"] .model-selector,
  .modal-dialog.model-selector-dialog .model-selector {
    height: 100%;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }
}
