/*
 * Next UI shell — scoped under body.next-ui (flag: ?next_ui=1 → localStorage.next_ui).
 * Plan: docs/plans/2026-08-22-next-ui-shell-implementation.md (Task 0).
 *
 * Layout: claude-chat/ui-layout.js wrapMainContent() re-parents the body
 * children (incl. #mainNavbar) into .app-wrapper > .main-content; the rail is
 * inserted directly before #mainAppContent. .main-content becomes a wrapping
 * flex row so the sticky navbar keeps a full-width line and the rail + page
 * container share the line below it. The page container gets a 0 flex basis:
 * #mainAppContent is .container-fluid (width: 100%), so `flex-basis: auto`
 * would make it wrap onto its own line below the rail.
 * .main-content { margin-right } (chat panel) is untouched.
 */

body.next-ui .main-content {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
}

/* Everything that is not the rail or a page container keeps a full line. */
body.next-ui .main-content > :not(#next-ui-rail):not(#mainAppContent):not(#settingsPageContainer) {
  flex: 0 0 100%;
}

body.next-ui #mainAppContent,
body.next-ui #settingsPageContainer {
  flex: 1 1 0;
  min-width: 0;
}

/* Rail */
body.next-ui #next-ui-rail {
  width: 88px;
  flex: 0 0 88px;
  background: #fff;
  border-right: 1px solid #dee2e6;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 56px; /* below the sticky #mainNavbar */
  align-self: flex-start;
  height: calc(100vh - 56px);
  overflow-y: auto;
  z-index: 1010;
}

body.next-ui .next-ui-rail-btn {
  appearance: none;
  background: transparent;
  border: 0;
  border-radius: 10px;
  width: 100%;
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: #495057;
  font-size: 11px;
  line-height: 1.2;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

body.next-ui .next-ui-rail-btn:hover,
body.next-ui .next-ui-rail-btn:focus-visible {
  background: #f1f3f5;
  color: #212529;
  outline: none;
}

body.next-ui .next-ui-rail-btn:focus-visible {
  box-shadow: 0 0 0 2px #0d6efd;
}

body.next-ui .next-ui-rail-btn.active {
  background: #e7f1ff;
  color: #0d6efd;
  font-weight: 600;
}

body.next-ui .next-ui-rail-icon {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 28px;
}

body.next-ui .next-ui-rail-icon svg {
  display: block;
}

body.next-ui .next-ui-rail-badge {
  position: absolute;
  top: -4px;
  right: -2px;
  font-size: 10px;
  min-width: 18px;
  padding: 2px 5px;
  line-height: 14px;
}

body.next-ui .next-ui-rail-label {
  max-width: 100%;
  /* wrapping/size: see the "Staging smoke tweaks" block below */
}

/* When the navbar is hidden (settings page) the rail takes the full height. */
body.next-ui:has(#mainNavbar.d-none) #next-ui-rail {
  top: 0;
  height: 100vh;
}

/*
 * The rail carries the destinations — hide the old nav buttons and ONLY the
 * view-switcher tabs that move into Inbox (Action Required / Communications /
 * Activity) or Automations (Choreography → the Map pane, stage D of
 * docs/plans/2026-08-24-unified-automations-destination.md; desktop btn-group
 * and the mobile overflow menu both live inside #viewSelectorRow).
 * Hierarchy / Kanban / Flow stay visible: they are the Work view switcher.
 */
body.next-ui #viewSelectorRow [data-view="action-required"],
body.next-ui #viewSelectorRow [data-view="communications"],
body.next-ui #viewSelectorRow [data-view="workspace-activity"],
body.next-ui #viewSelectorRow [data-view="choreography"],
body.next-ui #homeViewBtn,
body.next-ui #openSettingsBtn,
body.next-ui #workflowsBtnSlot,
body.next-ui #workflows-btn {
  display: none !important;
}

/*
 * Stage D: links to the standalone /choreography page rendered inside the
 * Automations page (the Flows pane's map/seams views share their markup with
 * the flag-off modal, which keeps its "Open in Choreography" links). Flag-on
 * the Map pane IS the choreography view, so the escape hatch is noise —
 * hidden here rather than removed so the flag-off modal stays byte-identical.
 */
body.next-ui #next-ui-automations a[href^="/choreography"] {
  display: none !important;
}

/*
 * Settings destination (Task 4): the user-menu items that became sections of
 * the settings page's Account group (profile, notifications, API keys,
 * subscription, BYOK → AI routing, bug report) are hidden; the shell inserts
 * one "Account" item (#nextUiAccountBtn) instead.
 */
body.next-ui #apiKeysBtn,
body.next-ui #subscriptionBtn,
body.next-ui #byokSettingsBtn,
body.next-ui #notificationSettingsBtn,
body.next-ui #profileSettingsBtn,
body.next-ui #bugReportBtn {
  display: none !important;
}

/*
 * The divider that sat between "My Profile" and "Report a Bug" is orphaned
 * once both are hidden (it would stack directly under the divider after the
 * user stats, above "Account"). Hide it too — targeted structurally so the
 * markup needs no next-ui-specific ids.
 */
body.next-ui #userDropdownMenu li:has(+ li > #bugReportBtn) {
  display: none !important;
}

/* Mobile: the rail becomes a bottom tab bar. */
@media (max-width: 767.98px) {
  body.next-ui #next-ui-rail {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    height: 64px;
    width: auto;
    flex: none;
    flex-direction: row;
    align-items: stretch;
    justify-content: space-around;
    gap: 0;
    padding: 4px 4px 6px;
    border-top: 1px solid #dee2e6;
    border-right: 0;
    overflow: visible;
    z-index: 1030;
  }

  body.next-ui .next-ui-rail-btn {
    flex: 1 1 0;
    padding: 4px 2px;
    font-size: 10px;
  }

  body.next-ui .main-content {
    display: block;
    padding-bottom: 64px;
  }
}

/* ---------------------------------------------------------------------------
 * Inbox destination (next-ui-inbox.js, Task 1)
 * ------------------------------------------------------------------------- */
body.next-ui #next-ui-inbox {
  padding: 4px 4px 24px;
}

body.next-ui .next-ui-inbox-header {
  position: sticky;
  top: 56px; /* below the sticky #mainNavbar */
  z-index: 5;
  background: #fff;
  border-bottom: 1px solid #dee2e6;
  margin-bottom: 12px;
}

body.next-ui .next-ui-inbox-title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 8px 8px 4px;
}

body.next-ui .next-ui-inbox-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
}

body.next-ui .next-ui-inbox-scope .btn {
  border-radius: 999px;
  font-size: 0.8rem;
  padding: 2px 12px;
}

body.next-ui .next-ui-inbox-scope .btn:first-child {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

body.next-ui .next-ui-inbox-scope .btn:last-child {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

body.next-ui .next-ui-inbox-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 0 8px;
}

body.next-ui .next-ui-inbox-tab {
  appearance: none;
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 8px 12px;
  color: #495057;
  font-size: 0.9rem;
  cursor: pointer;
  margin-bottom: -1px;
}

body.next-ui .next-ui-inbox-tab:hover,
body.next-ui .next-ui-inbox-tab:focus-visible {
  color: #212529;
  background: #f8f9fa;
  outline: none;
}

body.next-ui .next-ui-inbox-tab.active {
  color: #0d6efd;
  border-bottom-color: #0d6efd;
  font-weight: 600;
}

body.next-ui .next-ui-inbox-body {
  min-height: 200px;
}

/* The adopted view containers render directly under the tab strip. */
body.next-ui .next-ui-inbox-body > #home-view-container,
body.next-ui .next-ui-inbox-body > #action-required-container,
body.next-ui .next-ui-inbox-body > #communications-view-container,
body.next-ui .next-ui-inbox-body > #workspace-activity-container {
  width: 100%;
}

body.next-ui .next-ui-inbox-unavailable,
body.next-ui .next-ui-inbox-empty {
  padding: 24px 12px;
  font-size: 0.9rem;
}

body.next-ui .next-ui-inbox-pane-toolbar {
  display: flex;
  justify-content: flex-end;
  padding: 0 12px 8px;
}

body.next-ui .next-ui-inbox-list .notification-item {
  padding: 10px 12px;
  border-bottom: 1px solid #f1f3f5;
  cursor: pointer;
}

@media (max-width: 767.98px) {
  body.next-ui .next-ui-inbox-header {
    position: static;
  }
}

/* ---------------------------------------------------------------------------
 * Automations destination (next-ui-automations.js, Task 2)
 * The modal's tabs + panes scaffold rendered as a page: #automation-tabs is a
 * 200px left column (nav-pills flex-column), the panes take the rest.
 * ------------------------------------------------------------------------- */
body.next-ui #next-ui-automations {
  padding: 4px 4px 24px;
  min-height: calc(100vh - 120px);
}

body.next-ui #next-ui-automations .next-ui-automations-layout {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 12px;
}

body.next-ui #next-ui-automations #automation-tabs {
  flex: 0 0 200px;
  width: 200px;
  flex-direction: column;
  gap: 2px;
  position: sticky;
  top: 64px; /* below the sticky #mainNavbar */
  border: 0;
}

body.next-ui #next-ui-automations #automation-tabs .nav-link {
  width: 100%;
  text-align: left;
  border-radius: 8px;
  color: #495057;
  padding: 8px 12px;
}

body.next-ui #next-ui-automations #automation-tabs .nav-link:hover,
body.next-ui #next-ui-automations #automation-tabs .nav-link:focus-visible {
  background: #f1f3f5;
  color: #212529;
}

body.next-ui #next-ui-automations #automation-tabs .nav-link.active {
  background: #e7f1ff;
  color: #0d6efd;
  font-weight: 600;
}

body.next-ui #next-ui-automations .tab-content {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 320px;
}

body.next-ui #next-ui-automations .next-ui-automations-unavailable {
  padding: 24px 12px;
  font-size: 0.9rem;
}

@media (max-width: 767.98px) {
  body.next-ui #next-ui-automations .next-ui-automations-layout {
    flex-direction: column;
    gap: 8px;
    padding: 8px;
  }

  body.next-ui #next-ui-automations #automation-tabs {
    flex: none;
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    position: static;
  }

  body.next-ui #next-ui-automations #automation-tabs .nav-link {
    width: auto;
  }
}

/* ---------------------------------------------------------------------------
 * Apps destination (next-ui-apps.js, Task 3)
 * ------------------------------------------------------------------------- */
body.next-ui #next-ui-apps {
  padding: 4px 4px 24px;
}

body.next-ui .next-ui-apps-header {
  position: sticky;
  top: 56px; /* below the sticky #mainNavbar */
  z-index: 5;
  background: #fff;
  border-bottom: 1px solid #dee2e6;
  margin-bottom: 12px;
}

body.next-ui .next-ui-apps-title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 8px 8px 8px;
}

body.next-ui .next-ui-apps-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
}

body.next-ui .next-ui-apps-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

body.next-ui .next-ui-apps-filters .btn {
  font-size: 0.8rem;
  padding: 2px 12px;
}

body.next-ui .next-ui-apps-filters .btn:first-child {
  border-top-left-radius: 999px;
  border-bottom-left-radius: 999px;
}

body.next-ui .next-ui-apps-filters .btn:last-child {
  border-top-right-radius: 999px;
  border-bottom-right-radius: 999px;
}

body.next-ui .next-ui-apps-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0 8px;
}

body.next-ui .next-ui-apps-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 10px 12px;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  background: #fff;
}

body.next-ui .next-ui-apps-row:hover {
  background: #f8f9fa;
}

body.next-ui .next-ui-apps-row-main {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1 1 auto;
}

body.next-ui .next-ui-apps-row-title {
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

body.next-ui .next-ui-apps-badge {
  font-weight: 500;
  font-size: 0.7rem;
  white-space: nowrap;
}

body.next-ui .next-ui-apps-row-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

body.next-ui .next-ui-apps-unavailable,
body.next-ui .next-ui-apps-empty {
  padding: 24px 12px;
  font-size: 0.9rem;
}

body.next-ui .next-ui-apps-empty {
  text-align: center;
  border: 1px dashed #dee2e6;
  border-radius: 8px;
}

@media (max-width: 767.98px) {
  body.next-ui .next-ui-apps-header {
    position: static;
  }

  body.next-ui .next-ui-apps-row-actions {
    width: 100%;
    justify-content: flex-end;
  }
}

/* ---------------------------------------------------------------------------
 * Settings › Account › Workspaces (settings-sections.js, Wave 2 Task 4)
 * Rendered flag-off too (the Account group is shared), so not scoped to
 * body.next-ui. The create panel hosts the adopted #newWorkspaceModal body;
 * its fullscreen-modal setup cards wrap to a 2-up grid inside the 800px page.
 * ------------------------------------------------------------------------- */
.settings-workspace-row .badge {
  font-weight: 500;
}

.settings-workspace-create {
  border-top: 1px solid #dee2e6;
  padding-top: 16px;
}

.settings-workspace-create-host .workspace-setup-card .card-body {
  padding-top: 12px;
  padding-bottom: 12px;
}

.settings-workspace-create-host .workspace-setup-card .fs-1 {
  font-size: 1.5rem !important;
  margin-bottom: 8px !important;
}

@media (min-width: 768px) and (max-width: 991.98px) {
  .settings-workspace-create-host .row > .col-md-3 {
    flex: 0 0 50%;
    max-width: 50%;
  }
}

/* ---------------------------------------------------------------------------
 * Unified New modal (next-ui-new-modal.js)
 * ------------------------------------------------------------------------- */

body.next-ui .next-ui-new-tiles {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: 12px;
}

body.next-ui .next-ui-new-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 4px;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  background: #fff;
  color: #495057;
  font-size: 0.75rem;
  line-height: 1.1;
  cursor: pointer;
}

body.next-ui .next-ui-new-tile:hover {
  background: #f8f9fa;
  color: #212529;
}

body.next-ui .next-ui-new-tile.active,
body.next-ui .next-ui-new-tile[aria-pressed="true"] {
  background: #e7f1ff;
  border-color: #0d6efd;
  color: #0d6efd;
}

body.next-ui .next-ui-new-tile:focus-visible {
  outline: 2px solid #0d6efd;
  outline-offset: 1px;
}

body.next-ui .next-ui-new-tile-icon {
  display: inline-flex;
}

body.next-ui .next-ui-new-tile-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

body.next-ui .next-ui-new-parent {
  font-size: 0.85rem;
  color: #495057;
  margin-bottom: 12px;
}

body.next-ui .next-ui-new-parent-text {
  font-weight: 500;
}

@media (max-width: 767.98px) {
  body.next-ui .next-ui-new-tiles {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* Task 5: handler editor pane */
/* ---------------------------------------------------------------------------
 * Full-page handler editor on the Automations page (next-ui-automations.js
 * openHandlerEditor): the create/edit handler form rendered inline with a
 * Back button + heading, replacing the tab rail + panes while open.
 * ------------------------------------------------------------------------- */
body.next-ui #next-ui-automations .next-ui-handler-editor {
  padding: 12px;
  max-width: 960px;
  /* R4: a flex sibling of the sub-rail inside .next-ui-automations-layout —
     it takes the panes column's place while open, the rail stays put. */
  flex: 1 1 auto;
  min-width: 0;
}

/* R7: shared pane header (workflow-integration.js renderPaneHeader) — one
   component for Map · Workflows · Handlers · Flows · Runs on the page path. */
body.next-ui #next-ui-automations .next-ui-pane-header {
  padding: 0 0 12px;
  border-bottom: 1px solid #e9ecef;
}

body.next-ui #next-ui-automations .next-ui-pane-header-title {
  font-weight: 600;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
}

body.next-ui #next-ui-automations .next-ui-pane-header-desc {
  font-size: 0.9rem;
}

body.next-ui #next-ui-automations .next-ui-pane-header-meta {
  font-size: 0.7rem;
}

/* The Handlers list renders its own "+ New Handler" (list header + empty
   state); on the page the shared header carries the action instead. */
body.next-ui #next-ui-automations #handlers-container #handler-create-btn {
  display: none;
}

body.next-ui #next-ui-automations .next-ui-handler-editor-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

body.next-ui #next-ui-automations .next-ui-handler-editor-title {
  font-weight: 600;
}

body.next-ui #next-ui-automations .next-ui-handler-editor-body {
  background: #fff;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  padding: 16px;
}

body.next-ui #next-ui-automations .next-ui-handler-editor-body .handler-editor-footer {
  border-top: 1px solid #dee2e6;
  padding-top: 12px;
}

@media (max-width: 767.98px) {
  body.next-ui #next-ui-automations .next-ui-handler-editor {
    padding: 8px;
  }

  body.next-ui #next-ui-automations .next-ui-handler-editor-body {
    padding: 12px;
  }
}

/* ---------------------------------------------------------------------------
 * Task 6: apps editor pane (next-ui-apps.js) — full-page Surface / Form
 * builders rendered into #next-ui-apps-editor-container via { container }.
 * The builders keep their .modal-header/.modal-body/.modal-footer markup;
 * these rules give that markup a card frame and a scrollable body outside
 * of a bootstrap modal.
 * ------------------------------------------------------------------------- */
body.next-ui .next-ui-apps-editor-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 8px 12px;
}

body.next-ui .next-ui-apps-editor-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

body.next-ui .next-ui-apps-editor-container {
  display: flex;
  flex-direction: column;
  margin: 0 8px;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  background: #fff;
  min-height: calc(100vh - 160px);
}

body.next-ui .next-ui-apps-editor-container > .modal-header {
  border-bottom: 1px solid #dee2e6;
}

body.next-ui .next-ui-apps-editor-container > .modal-body {
  flex: 1 1 auto;
  min-height: 0;
  max-height: calc(100vh - 260px);
}

body.next-ui .next-ui-apps-editor-container > .modal-footer {
  border-top: 1px solid #dee2e6;
}

@media (max-width: 767.98px) {
  body.next-ui .next-ui-apps-editor-container {
    margin: 0;
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    min-height: 0;
  }

  body.next-ui .next-ui-apps-editor-container > .modal-body {
    max-height: none;
  }
}

/* ── Work: block panel inline title edit (Wave 2 Task 7) ───────────────── */
body.next-ui .ubv-content-title.ubv-title-editable {
  cursor: text;
  border-radius: 4px;
  margin-left: -0.25rem;
  padding-left: 0.25rem;
  padding-right: 0.25rem;
}

body.next-ui .ubv-content-title.ubv-title-editable:hover,
body.next-ui .ubv-content-title.ubv-title-editable:focus-visible {
  background: rgba(0, 0, 0, 0.04);
  outline: none;
}

body.next-ui .ubv-title-inline-input {
  font-size: inherit;
  font-weight: inherit;
  line-height: 1.2;
}

/* ── Work: row "⋯" menu — folded "More…" items ─────────────────────────── */
body.next-ui .dropdown-menu li.next-ui-more-item .dropdown-item {
  padding-left: 1.5rem;
}

/* ── Staging smoke tweaks 2026-08-23 ─────────────────────────────────────── */
/* Rail labels: "Automations" must fit the 88px rail without truncating
   (polish Stage 3, audit F9: it still ellipsised to "Automatio…" at the
   default width). Never clip — let a long label wrap to a second line at a
   slightly smaller size; the other four labels are single short words. */
body.next-ui .next-ui-rail-label {
  font-size: 0.625rem;
  letter-spacing: -0.01em;
  white-space: normal;
  overflow: visible;
  text-overflow: clip;
  overflow-wrap: normal;
  text-align: center;
  line-height: 1.15;
}
/* Left-positioned chat toggle sat on top of the rail; push it past the rail
   while the drawer is closed (the open state is handled by main.css). */
body.next-ui:has(.claude-chat-container.chat-position-left:not(.show)) .claude-chat-toggle {
  left: 88px !important;
  right: auto !important;
  border-radius: 0 8px 8px 0;
}
/* Inside the Inbox the Home view's own page header duplicates the tab header. */
body.next-ui #next-ui-inbox .hv-page-header {
  display: none;
}
/* 2px short for "Automations" at 11px — give the label the button's side padding. */
body.next-ui .next-ui-rail-btn {
  padding-left: 2px;
  padding-right: 2px;
}
