/* ComputaCo-style floating Settings & Connections modal (settings-modal.js).
   Desktop (> 760px): centered floating dialog over the workspace — soft --setm-frame left nav
   + crisp --setm-sheet right pane, 56px title bar, naked × close, zero single-side borders.
   Mobile (<= 760px): fullscreen popup switching between section list (.setm.nav) and section page. */

.setm-bg {
  position: fixed;
  inset: 0;
  z-index: 38;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.42);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  animation: setm-fade 160ms ease;
}

.setm {
  --setm-frame: hsl(var(--bg-dock));
  --setm-frame-hover: hsl(var(--bg-dock-2));
  --setm-frame-active: hsl(var(--bg-dock-3));
  --setm-sheet: hsl(var(--bg-1));
  --setm-search-bg: hsl(var(--bg-0) / 0.72);
  width: min(1040px, calc(100vw - 32px));
  height: min(760px, calc(100svh - 32px));
  background: var(--setm-sheet);
  color: hsl(var(--text-1));
  border-radius: 16px;
  box-shadow:
    0 40px 100px -30px rgba(0, 0, 0, 0.5),
    0 12px 32px -16px rgba(0, 0, 0, 0.25),
    0 0 0 1px hsl(var(--border-2) / 0.6);
  display: grid;
  grid-template-columns: 252px minmax(0, 1fr);
  overflow: hidden;
  animation: setm-rise 200ms cubic-bezier(.22, 1, .36, 1);
}

:root[data-theme=light] .setm {
  --setm-frame: hsl(var(--bg-dock));
  --setm-frame-hover: hsl(var(--bg-dock-2));
  --setm-frame-active: hsl(var(--bg-dock-3));
  --setm-sheet: hsl(var(--bg-0));
  --setm-search-bg: hsl(var(--bg-0));
  box-shadow:
    0 40px 100px -30px rgba(0, 0, 0, 0.36),
    0 12px 32px -16px rgba(0, 0, 0, 0.18),
    0 0 0 1px hsl(var(--border-1) / 0.85);
}

/* Left section list on soft --setm-frame ground (tonal separation, no single-side border) */
.setm__nav {
  background: var(--setm-frame);
  padding: 16px 12px 20px;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  min-height: 0;
}

.setm__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 8px 0;
  flex: none;
}

.setm__title {
  font-size: var(--fs-title, 17px);
  font-weight: 600;
  letter-spacing: -0.02em;
  color: hsl(var(--text-1));
  line-height: 1.3;
}

/* Account scope switcher rail (mounted above sections when an account section is active) */
.setm__account-rail {
  flex: none;
  padding: 0 2px 4px;
}

.setm__account-rail[hidden] {
  display: none;
}

.setm__account-rail [data-settings-rail] nav {
  display: none;
}

/* Soft rounded search bar inside the nav frame */
.setm__search-wrap {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.setm__search {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 34px;
  padding: 0 10px;
  border-radius: 8px;
  background: var(--setm-search-bg);
  color: hsl(var(--text-3));
  box-shadow: 0 0 0 1px hsl(var(--border-1) / 0.7);
  transition: box-shadow 120ms ease, color 120ms ease;
}

.setm__search:focus-within {
  color: hsl(var(--text-1));
  box-shadow: 0 0 0 1.5px hsl(var(--brand) / 0.65);
}

.setm__search-input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  padding: 0;
  background: transparent;
  color: hsl(var(--text-1));
  font: var(--fs-ui, 14px) var(--font-sans);
  box-shadow: none;
}

.setm__search-input:hover,
.setm__search-input:focus,
.setm__search-input:focus-visible {
  border: 0;
  box-shadow: none;
}

.setm__search-input::-webkit-search-cancel-button {
  display: none;
}

.setm__search-clear {
  flex: none;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  display: inline-grid;
  place-items: center;
  background: transparent;
  color: hsl(var(--text-3));
}

.setm__search-clear:hover {
  background: var(--setm-frame-hover);
  color: hsl(var(--text-1));
}

.setm__results {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 0;
}

.setm__results[hidden] {
  display: none;
}

.setm__result {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: hsl(var(--text-2));
  font: var(--fs-ui, 14px) var(--font-sans);
  text-align: start;
}

.setm__result:hover {
  background: var(--setm-frame-hover);
  color: hsl(var(--text-1));
}

.setm__result-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.setm__result-group {
  flex: none;
  font-size: var(--fs-caption, 13px);
  color: hsl(var(--text-3));
}

.setm__none {
  padding: 6px 10px;
  font: var(--fs-caption, 13px) var(--font-sans);
  color: hsl(var(--text-3));
}

/* Grouped navigation list with ComputaCo soft pill active state (zero border stripes) */
.setm__navlist {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0;
}

.setm__group {
  flex: none;
  padding: 12px 10px 4px;
  font: 600 var(--fs-micro, 11.5px)/1.2 var(--font-sans);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: hsl(var(--text-2));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.setm__group:first-child {
  padding-top: 4px;
}

.setm__navlist .setm__item {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: hsl(var(--text-2));
  font: 400 var(--fs-ui, 14px)/1.3 var(--font-sans);
  text-align: start;
  white-space: nowrap;
  overflow: hidden;
  transition: background-color 110ms ease, color 110ms ease;
}

.setm__navlist .setm__item .icon {
  flex: none;
  color: hsl(var(--text-3));
}

.setm__navlist .setm__item-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.setm__navlist .setm__item:hover {
  background: var(--setm-frame-hover);
  color: hsl(var(--text-1));
}

.setm__navlist .setm__item:hover .icon {
  color: hsl(var(--text-1));
}

.setm__navlist .setm__item[aria-current="page"],
.setm__navlist .setm__item.on {
  background: var(--setm-frame-active);
  color: hsl(var(--text-1));
  font-weight: 500;
}

.setm__navlist .setm__item[aria-current="page"] .icon,
.setm__navlist .setm__item.on .icon {
  color: hsl(var(--text-1));
}

.setm__top-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-bottom: 4px;
}

/* Right content sheet + spacious 56px title bar */
.setm__page {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--setm-sheet);
}

.setm__bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 56px;
  padding: 0 14px 0 28px;
}

.setm__title-h {
  flex: 1;
  min-width: 0;
  margin: 0;
  font: 600 var(--fs-heading, 21px)/1.25 var(--font-sans);
  letter-spacing: -0.02em;
  color: hsl(var(--text-1));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.setm__title-h:focus {
  outline: none;
}

/* Naked icon buttons for close (×) and mobile back (<) */
.setm__iconbtn {
  flex: none;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  display: inline-grid;
  place-items: center;
  background: transparent;
  color: hsl(var(--text-3));
  cursor: pointer;
}

.setm__iconbtn:hover {
  background: hsl(var(--bg-2));
  color: hsl(var(--text-1));
}

.setm__back,
.setm__navx {
  display: none;
}

/* Scrollable body inside the right sheet */
.setm__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
}

.setm__body--flush {
  overflow: hidden;
}

.setm__settings-host {
  display: flex;
  flex-direction: column;
  flex: 1 0 auto;
  min-height: 0;
}

.setm__settings-host:has(.st-account) {
  flex: 1;
  height: 100%;
}

/* Settings content inside the modal: inherits the modal's sheet ground, no double heading, single scroller */
.setm .st {
  background: transparent;
  flex: 1 0 auto;
  height: auto;
}

.setm .st:has(.st-account) {
  flex: 1;
  height: 100%;
}

.setm .st-body {
  overflow: visible;
  flex: none;
}

.setm .st:has(.st-account) .st-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.setm .st-section {
  padding: 4px 28px 36px;
  max-width: 740px;
  margin-inline: 0;
}

.setm .st-section > .pane-title {
  display: none;
}

/* Mounted React apps (Account settings & Connections) inherit the modal's sheet background */
.setm .cagi-app {
  --bg: var(--setm-sheet);
  height: 100%;
}

/* Connections view inside the modal's spacious right sheet */
.setm__connections {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.setm__connections > .live-surface,
.setm__connections > .sheet-surface {
  flex: 1;
  min-height: 0;
  height: 100%;
}

.setm__connections [data-connections] > nav {
  width: 204px;
  margin: 0 0 16px 20px;
  border-radius: 12px;
}

.setm__connections [data-connections] > [data-page] {
  padding-top: 8px;
}

.setm__connections-fallback {
  padding: 4px 28px 32px;
  max-width: 720px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.setm__connections-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

/* Mobile (<= 760px): fullscreen popup switching between section list (.setm.nav) and section page */
@media (max-width: 760px) {
  .setm-bg {
    padding: 0;
    background: var(--setm-sheet);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  .setm {
    width: 100%;
    height: 100svh;
    max-height: none;
    border-radius: 0;
    box-shadow: none;
    grid-template-columns: minmax(0, 1fr);
    animation: none;
  }

  .setm .setm__nav {
    display: none;
  }

  .setm.nav .setm__nav {
    display: flex;
    padding: calc(14px + env(safe-area-inset-top)) 14px calc(24px + env(safe-area-inset-bottom));
  }

  .setm.nav .setm__page {
    display: none;
  }

  .setm.nav .setm__navx {
    display: inline-grid;
  }

  .setm__navlist .setm__item {
    height: 44px;
    font-size: 15px;
    padding: 0 12px;
  }

  .setm__back {
    display: inline-grid;
  }

  .setm__bar {
    height: calc(56px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 10px 0;
  }

  .setm .st-section {
    padding: 4px 16px calc(28px + env(safe-area-inset-bottom));
    max-width: none;
  }

  .setm__connections [data-connections] > nav {
    width: auto;
    margin: 4px 12px 0;
  }

  .setm__connections-fallback {
    padding: 4px 16px calc(28px + env(safe-area-inset-bottom));
  }
}

@keyframes setm-fade {
  from { opacity: 0; }
}

@keyframes setm-rise {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .setm-bg,
  .setm,
  .setm__navlist .setm__item,
  .setm__search {
    animation: none;
    transition: none;
  }
}
