/**
 * Phase 20A.5 — mobile shell: off-canvas sidebar drawer.
 * Loaded after app.css; desktop layout unchanged (min-width: 769px).
 */

/* ---- Mobile drawer (≤768px) ---- */
@media (max-width: 768px) {
  :root {
    --sc-topbar-h: 48px;
  }

  .sc-shell.sc-shell-mobile,
  .sc-app-shell.sc-shell-mobile {
    grid-template-columns: 1fr;
    grid-template-rows: var(--sc-topbar-h) 1fr;
    grid-template-areas:
      "topbar"
      "content";
    min-height: 100vh;
    min-height: 100dvh;
  }

  .sc-mobile-topbar {
    padding: 0 12px;
    padding-top: env(safe-area-inset-top, 0);
    gap: 8px;
    flex-wrap: nowrap;
    min-height: var(--sc-topbar-h);
  }

  .sc-topbar-main {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    flex: 1 1 auto;
  }

  .sc-sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 1px solid var(--sc-hairline, #ece9e0);
    border-radius: 8px;
    background: var(--surface-card, #fff);
    color: var(--foreground, #1c1f24);
    cursor: pointer;
  }

  .sc-sidebar-toggle:focus-visible {
    outline: 2px solid var(--primary, #1f6f55);
    outline-offset: 2px;
  }

  .sc-sidebar-toggle__icon {
    display: block;
  }

  .sc-mobile-topbar .sc-topbar-title {
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
  }

  .sc-mobile-topbar .sc-topbar-user {
    flex-shrink: 0;
    gap: 6px;
    flex-wrap: nowrap;
  }

  .sc-mobile-topbar .sc-user-pill {
    display: none;
  }

  .sc-mobile-topbar .sc-company-pill {
    max-width: 88px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 11px;
    padding: 3px 8px;
  }

  .sc-mobile-topbar .sc-demo-badge,
  .sc-mobile-topbar .sc-demo-showcase-badge {
    font-size: 10px;
    padding: 2px 6px;
  }

  .sc-mobile-topbar .sc-theme-toggle__label {
    display: none;
  }

  .sc-mobile-topbar .sc-logout {
    padding: 5px 10px;
    font-size: 12px;
  }

  /* Override Phase 6A horizontal sidebar — drawer instead */
  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1200;
    width: min(280px, 85vw);
    max-width: 280px;
    height: 100vh;
    height: 100dvh;
    margin: 0;
    padding: calc(12px + env(safe-area-inset-top, 0)) 12px calc(16px + env(safe-area-inset-bottom, 0));
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 4px;
    border-right: 1px solid var(--sc-sidebar-border, #ece9e0);
    border-bottom: 0;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-shadow: 4px 0 24px rgba(15, 18, 22, 0.12);
  }

  body.sc-sidebar-open .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer {
    transform: translateX(0);
  }

  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-sidebar-nav,
  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-nav {
    display: block;
    width: 100%;
    overflow-y: visible;
    flex-wrap: nowrap;
  }

  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-sidebar-section-label,
  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-sidebar-section,
  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-nav .sc-section {
    width: auto;
    margin-top: 10px;
  }

  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-sidebar-link.is-active::before,
  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-sidebar-link-active::before,
  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-nav a.is-active::before {
    display: block;
  }

  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer .sc-sidebar-footer {
    display: flex;
  }

  .sc-sidebar-drawer__head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 4px;
  }

  .sc-sidebar-drawer__head .sc-sidebar-brand {
    flex: 1;
    min-width: 0;
    margin-bottom: 0;
    padding-bottom: 8px;
  }

  .sc-sidebar-drawer-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    margin-top: 4px;
    padding: 0;
    border: 1px solid var(--sc-hairline, #ece9e0);
    border-radius: 8px;
    background: var(--surface-muted, #f8f6f1);
    color: var(--foreground, #1c1f24);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
  }

  .sc-sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1190;
    background: rgba(15, 18, 22, 0.42);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.22s ease;
  }

  body.sc-sidebar-open .sc-sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .sc-app-shell .sc-content {
    padding: 12px 14px 20px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0));
  }
}

/* Desktop: hide mobile-only controls */
@media (min-width: 769px) {
  .sc-sidebar-toggle,
  .sc-sidebar-drawer-close,
  .sc-sidebar-backdrop {
    display: none !important;
  }

  .sc-sidebar-drawer__head {
    display: contents;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sc-app-shell .sc-sidebar.sc-sidebar--v2.sc-sidebar-drawer,
  .sc-sidebar-backdrop {
    transition: none;
  }
}

html[data-theme="dark"] .sc-sidebar-toggle,
html[data-theme="dark"] .sc-sidebar-drawer-close {
  background: var(--surface-card, #1a1d21);
  border-color: var(--sc-hairline, #2c3239);
  color: var(--foreground, #e6e7e8);
}

html[data-theme="dark"] body.sc-sidebar-open .sc-sidebar-backdrop {
  background: rgba(0, 0, 0, 0.58);
}
