/*
 * Maestria app shell: sidebar (desktop), top bar + bottom nav (mobile/tablet),
 * page frame and page-level layouts. Depends on tokens, base, components.
 *
 * Breakpoints: mobile first; 640px (two-column content), 1024px (sidebar shell).
 * Content layouts use container queries so they adapt to the space left by the sidebar.
 */

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr;
  min-height: 100dvh;
}

/* ---------- Brand ---------- */

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: var(--fs-md);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-tight);
  text-decoration: none;
}
.brand:hover { text-decoration: none; }
.brand__mark { width: 26px; height: 26px; flex: none; }
.brand__bg { fill: var(--ink); }
.brand__bars { fill: none; stroke: var(--on-ink); stroke-width: 2.1; stroke-linecap: round; }

/* ---------- Top bar (mobile, tablet) ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(var(--space-3), env(safe-area-inset-right)) 0 max(var(--space-3), env(safe-area-inset-left));
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topbar__end { margin-left: auto; display: flex; align-items: center; gap: var(--space-1); }

/* Scope switcher: "All workspaces" or one workspace. Mobile only. */
.scope { min-width: 0; }
.scope > summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  height: 36px;
  padding: 0 var(--space-2) 0 var(--space-3);
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  background: var(--surface);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
}
.scope > summary .icon { width: 16px; height: 16px; color: var(--text-3); }
.scope__all-icon { color: var(--text-2); }
.scope .menu__panel { left: 0; width: min(300px, calc(100vw - 24px)); }

.conn-dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap);
  height: var(--tap);
  border-radius: var(--radius-md);
  color: var(--text-2);
  text-decoration: none;
}
.conn-dot:hover { background: var(--surface-2); }

/* ---------- Theme toggle ---------- */

.theme-toggle .icon { display: none; }
:root:not([data-theme]) .theme-toggle .i-auto,
:root[data-theme="light"] .theme-toggle .i-light,
:root[data-theme="dark"] .theme-toggle .i-dark { display: block; }
.topbar .theme-toggle { width: var(--tap); height: var(--tap); }

/* ---------- Bottom nav (mobile, tablet) ---------- */

.bottomnav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--surface); /* opaque: list content must never show behind the labels */
  border-top: 1px solid var(--line-strong);
}
.bottomnav__item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--text-3);
  font-size: var(--fs-2xs);
  font-weight: var(--fw-medium);
  text-decoration: none;
}
.bottomnav__item .icon { width: 22px; height: 22px; }
.bottomnav__item:hover { color: var(--text-2); text-decoration: none; }
.bottomnav__item[aria-current="page"] { color: var(--text); }
.bottomnav__item[aria-current="page"] .icon { stroke-width: 2.1; }
.bottomnav__item[aria-current="page"]::before {
  content: "";
  position: absolute;
  top: 0;
  left: 30%;
  right: 30%;
  height: 2px;
  border-radius: 0 0 2px 2px;
  background: var(--ink);
}
.bottomnav__item .count { position: absolute; top: 5px; left: calc(50% + 5px); height: 17px; min-width: 17px; padding: 0 4px; }
.bottomnav__item:focus-visible { outline-offset: -4px; }

/* ---------- Sidebar (desktop) ---------- */

.sidebar, .sidebar-resizer { display: none; }

.nav { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1px; }
.nav__group {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: var(--space-5) 0 var(--space-1);
  padding: 0 var(--space-3);
  color: var(--text-3);
  font-size: var(--fs-2xs);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
}
.nav__item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 34px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-md);
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  text-decoration: none;
}
.nav__item > .icon { width: 17px; height: 17px; flex: none; color: var(--text-3); }
/* A name that may be long (workspace names): ellipsis, never a wider sidebar. */
.nav__label { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav__item:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav__item[aria-current="page"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1), inset 0 0 0 1px var(--line); }
.nav__item[aria-current="page"] > .icon { color: var(--text); }
.nav__item .count { margin-left: auto; flex: none; }
.nav__item .swatch { margin: 0 4px 0 5px; flex: none; }
.nav__sub { display: grid; gap: 1px; margin: 2px 0 var(--space-2) 18px; padding-left: 10px; border-left: 1px solid var(--line); }
.nav__sub .nav__item { min-height: 30px; }

.sidebar__foot { margin-top: auto; display: grid; gap: var(--space-2); padding-top: var(--space-4); }
.conn-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 2px var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-size: var(--fs-xs);
  text-decoration: none;
}
.conn-card:hover { border-color: var(--line-strong); text-decoration: none; }
.conn-card__name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: var(--fw-medium); }
.conn-card__ver { color: var(--text-3); font-variant-numeric: tabular-nums; white-space: nowrap; }
.conn-card__meta { grid-column: 2 / -1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-3); }

/* The signed-in account: avatar, the name over the version, then the actions. Only the text
   column shrinks; everything else keeps its size, so no value can widen the sidebar. */
.account-row { display: flex; align-items: center; gap: var(--space-2); min-width: 0; padding: 0 var(--space-1); }
.account-row > * { flex: none; }
.account-row > form { display: flex; }
.account-row__id { flex: 1 1 0; min-width: 0; display: grid; }
.account-row__name { display: block; min-width: 0; color: var(--text-2); font-size: var(--fs-sm); font-weight: var(--fw-medium); }
.account-row__ver {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
  color: var(--text-3);
  font-size: var(--fs-2xs);
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
  white-space: nowrap;
}
.account-row__ver > span:first-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
/* A newer release exists: just a dot after the version (the title says which). */
.ver-dot { position: relative; flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--tone); }
.ver-dot::after { content: ""; position: absolute; inset: -6px; }

/* A subtle "newer release" marker: a quiet word next to a version, in the info tone. */
.ver-hint { display: inline-flex; align-items: center; gap: 5px; color: var(--tone-ink); font-size: var(--fs-2xs); font-weight: var(--fw-medium); white-space: nowrap; }
.ver-hint::before { content: ""; flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--tone); }

/* ---------- Main and page frame ---------- */

.main {
  min-width: 0;
  padding: var(--space-4) max(var(--space-4), env(safe-area-inset-right)) calc(var(--bottomnav-h) + env(safe-area-inset-bottom) + var(--space-6)) max(var(--space-4), env(safe-area-inset-left));
}

.page {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-5);
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  container-type: inline-size;
}
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--space-2) var(--space-4); }
.page-head__title { display: grid; gap: 2px; min-width: 0; }
.page-head__sub { color: var(--text-2); font-size: var(--fs-sm); }
.page-head__actions { margin-left: auto; display: flex; align-items: center; gap: var(--space-2); }

.section { display: grid; gap: var(--space-3); min-width: 0; }
.section__head { display: flex; align-items: center; gap: var(--space-2); }
.section__title { font-size: var(--fs-base); font-weight: var(--fw-semibold); }
.section__head .panel__aside { margin-left: auto; }

@media (min-width: 640px) {
  .main { padding-left: var(--space-6); padding-right: var(--space-6); }
}

@media (min-width: 1024px) {
  .app { grid-template-columns: var(--sidebar-w) minmax(0, 1fr); grid-template-rows: none; }
  .topbar, .bottomnav { display: none; }
  .sidebar {
    position: sticky;
    top: 0;
    display: flex;
    flex-direction: column;
    height: 100dvh;
    padding: var(--space-4) var(--space-3);
    overflow-x: hidden; /* overflow-y: auto makes x auto too; the sidebar never scrolls sideways */
    overflow-y: auto;
    border-right: 1px solid var(--line);
    scrollbar-width: thin;
  }
  .sidebar > * { min-width: 0; }
  /* Explicit cells: the resizer shares the sidebar's, and auto-placement would skip it. */
  .sidebar { grid-area: 1 / 1; }
  .main { grid-area: 1 / 2; }

  /* Drag handle on the sidebar's right edge (see sidebar.js). It lives in the grid, over the
     sidebar's last pixels, so it stays put while the sidebar scrolls. */
  .sidebar-resizer[hidden] { display: none; }
  .sidebar-resizer {
    display: block;
    position: sticky;
    top: 0;
    grid-area: 1 / 1;
    align-self: start;
    justify-self: end;
    z-index: 20;
    width: 9px;
    height: 100dvh;
    margin-right: -5px;
    cursor: col-resize;
    touch-action: none;
    user-select: none;
  }
  .sidebar-resizer::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 4px;
    width: 2px;
    border-radius: 1px;
    background: transparent;
    transition: background var(--dur-fast) var(--ease);
  }
  .sidebar-resizer:hover::after { left: 3px; width: 3px; background: color-mix(in oklab, var(--focus) 50%, var(--line-strong)); }
  .sidebar-resizer:focus-visible { outline: none; }
  .sidebar-resizer:focus-visible::after, .is-resizing .sidebar-resizer::after { left: 3px; width: 3px; background: var(--focus); }
  .is-resizing, .is-resizing * { cursor: col-resize !important; user-select: none !important; }
  .sidebar > .brand { padding: var(--space-1) var(--space-3) var(--space-2); }
  .main { padding: var(--space-6) var(--space-7) var(--space-8); }
  .page-head h1 { font-size: var(--fs-2xl); }
}

/* ---------- Overview layout ---------- */

.ws-strip {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(176px, max-content);
  gap: var(--space-2);
  margin: 0 calc(var(--space-4) * -1);
  padding: 2px var(--space-4) var(--space-1);
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scroll-padding-inline: var(--space-4);
  scrollbar-width: none;
}
.ws-strip::-webkit-scrollbar { display: none; }
.ws-strip > * { scroll-snap-align: start; }

.overview-cols { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-4); align-items: start; }
.overview-cols > * { min-width: 0; }
.overview-main { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-4); align-items: start; }
.overview-side { display: grid; gap: var(--space-4); align-items: start; min-width: 0; }

@container (min-width: 600px) {
  .ws-strip {
    grid-auto-flow: row;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    margin: 0;
    padding: 0;
    overflow: visible;
  }
  .overview-side { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .overview-side > .span-all { grid-column: 1 / -1; }
}

@container (min-width: 900px) {
  .overview-cols { grid-template-columns: minmax(0, 1.4fr) minmax(320px, 1fr); }
  .overview-side { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- Connector indicator in the top bar ---------- */

.conn-dot { position: relative; }
.conn-dot .dot { position: absolute; top: 11px; right: 10px; box-shadow: 0 0 0 2px var(--bg); }

.nav__empty { padding: var(--space-1) var(--space-3); color: var(--text-3); font-size: var(--fs-xs); }

/* ---------- Page variants ---------- */

.page--narrow { max-width: 880px; }

.ws-eyebrow {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
}
.ws-eyebrow .status { font-weight: var(--fw-regular); }

.grid-cards { display: grid; gap: var(--space-3); grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); align-items: start; }

.summary-row { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); color: var(--text-2); font-size: var(--fs-sm); }

/* Floors grouped in Activity */
.floor-head { flex: 1; display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3); min-width: 0; }
.floor-head .floor-line__name { font-size: var(--fs-base); font-weight: var(--fw-semibold); }
.floor-head__progress { flex: 1 1 120px; max-width: 200px; }

/* ---------- Chat page ---------- */

/* The chat page fills the viewport between the top bar and the bottom nav: the header (title +
   thread selector) and the composer stay put, and only the message list scrolls. */
.chat-page {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--space-3);
  height: calc(100dvh - var(--topbar-h) - var(--space-4) - var(--bottomnav-h) - env(safe-area-inset-bottom));
  overflow: hidden;
}
.chat-page > .chat,
.chat-page > .chat-local {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 var(--space-1) var(--space-3) 0;
}
.chat-page .chat { align-content: safe end; } /* short chats sit at the bottom; long ones scroll from the top */
.chat-dock {
  position: relative;
  display: grid;
  gap: var(--space-2);
  padding: var(--space-2) 0 var(--space-3);
  background: var(--bg); /* opaque: messages never show through */
}
.chat-dock::before { /* soft fade above the dock as messages scroll under it */
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 100%;
  height: var(--space-5);
  background: linear-gradient(to bottom, transparent, var(--bg));
  pointer-events: none;
}
.chat-dock__hint { color: var(--text-3); font-size: var(--fs-xs); text-align: center; }
.chat-dock__jump { /* "jump to latest": floats above the dock while the reader is scrolled up */
  position: absolute;
  right: 0;
  bottom: calc(100% + var(--space-3));
  z-index: 1;
  gap: var(--space-1);
  background: var(--surface);
  box-shadow: var(--shadow-2);
}
.chat-dock__jump[hidden] { display: none; }
.chat-dock__jump[data-new] { border-color: var(--focus); color: var(--text); }
.main:has(.chat-page) { padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom)); }
.chat-top { display: grid; gap: var(--space-3); }

/* Thread selector: one swatch per Maestri chat thread (seven colours). A filled dot has
   messages, a ring is empty, a badge is unread; the thread shown is outlined. Names show
   for the current thread, and for all of them once there is room. */
.threads { display: grid; gap: var(--space-2); }
.threads__list { display: flex; flex-wrap: wrap; gap: var(--space-1); list-style: none; margin: 0; padding: 0; }
.thread {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  position: relative;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  background: var(--surface);
  color: var(--text-3);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  white-space: nowrap;
  text-decoration: none;
  transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
a.thread:hover { background: var(--surface-2); color: var(--text); }
.thread__dot {
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--tone);
  background: transparent; /* empty thread: a ring */
}
.thread[data-has] { color: var(--text-2); }
.thread[data-has] .thread__dot { background: var(--tone); }
.thread[aria-current] {
  border-color: var(--tone);
  background: var(--tone-tint);
  color: var(--tone-ink);
}
.thread[aria-disabled] { opacity: 0.55; cursor: not-allowed; }
.thread__badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--tone);
  box-shadow: 0 0 0 2px var(--bg);
}
.thread:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.thread__name { display: none; }
.thread[aria-current] .thread__name { display: inline; }
.threads__note { color: var(--text-2); font-size: var(--fs-xs); }
@container (min-width: 520px) { .thread__name { display: inline; } }

/* No live Chat (Wire off): the notice, then the saved log, read-only. */
.chat-page .chat-local { display: grid; align-content: start; gap: var(--space-4); }
.chat-local__title { font-size: var(--fs-sm); font-weight: var(--fw-semibold); color: var(--text-2); }
.chat-local__log { display: grid; gap: var(--space-3); }

/* ---------- Docs page ---------- */

.docs-layout { display: grid; gap: var(--space-4); align-items: start; }
.docs-nav { padding: var(--space-2); }
.docs-nav > summary { display: flex; align-items: center; gap: var(--space-2); min-height: 36px; padding: 0 var(--space-2); font-size: var(--fs-sm); font-weight: var(--fw-semibold); cursor: pointer; list-style: none; }
.docs-nav > summary::-webkit-details-marker { display: none; }
.docs-nav > summary .icon:last-child { margin-left: auto; color: var(--text-3); }
.docs-nav[open] > summary { margin-bottom: var(--space-1); }
.docs-body { padding: var(--space-5) var(--space-4); min-width: 0; }
.docs-meta { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin-bottom: var(--space-5); color: var(--text-3); font-size: var(--fs-xs); }
@container (min-width: 760px) {
  .docs-layout { grid-template-columns: 240px minmax(0, 1fr); }
  .docs-nav { position: sticky; top: var(--space-6); }
  .docs-nav > summary { pointer-events: none; }
  .docs-nav > summary .icon:last-child { display: none; }
  .docs-body { padding: var(--space-7) var(--space-8); }
}

/* ---------- Auth layout ---------- */

.auth {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: var(--space-6);
  min-height: 100dvh;
  padding: max(var(--space-7), env(safe-area-inset-top)) var(--space-4) max(var(--space-6), env(safe-area-inset-bottom));
}
.auth__brand { font-size: var(--fs-lg); }
.auth-card { display: grid; gap: var(--space-5); width: min(420px, 100%); padding: var(--space-6) var(--space-5); }
.auth-card--wide { width: min(560px, 100%); }
.auth-card__head { display: grid; gap: var(--space-2); }
.auth-card__head h1 { font-size: var(--fs-xl); }
.auth-card__head p { color: var(--text-2); font-size: var(--fs-sm); }
.auth-card form { display: grid; gap: var(--space-4); }
.auth-card__alt { color: var(--text-2); font-size: var(--fs-sm); text-align: center; }
.auth-card__row { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); font-size: var(--fs-sm); }
.auth__foot { display: flex; align-items: center; gap: var(--space-3); color: var(--text-3); font-size: var(--fs-xs); }
.qr-row { display: grid; justify-items: center; gap: var(--space-4); }
@media (min-width: 640px) {
  .auth-card { padding: var(--space-7); }
  .qr-row { grid-template-columns: auto minmax(0, 1fr); justify-items: start; align-items: center; }
}
@media (min-width: 1024px) {
  .chat-page { height: calc(100dvh - var(--space-6)); }
  .chat-dock { padding-bottom: var(--space-5); }
  .main:has(.chat-page) { padding-bottom: 0; }
}
