/* =============================================================================
   Layout — app shell: sidebar, topbar, content, mobile tab bar, nav drawer, page.
   Breakpoints: <768 phone (topbar + tab bar) · 768–1023 icon rail · ≥1024 sidebar.
   ============================================================================= */

/* ---------- Boot splash (before main.js renders) ---------- */
.boot {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  align-content: center;
  gap: var(--s-4);
  color: var(--text-3);
  font-size: var(--fs-sm);
}
.boot__mark { width: 44px; height: 44px; animation: boot-pulse 1.4s var(--ease) infinite; }
@keyframes boot-pulse { 50% { opacity: 0.45; transform: scale(0.96); } }

/* ---------- App grid ---------- */
.app {
  --sidebar-current: var(--sidebar-w);
  display: grid;
  grid-template-columns: var(--sidebar-current) minmax(0, 1fr);
  min-height: 100dvh;
}
.app[data-sidebar="collapsed"] { --sidebar-current: var(--sidebar-w-collapsed); }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 100dvh; }

/* ---------- Sidebar ---------- */
.sidebar {
  position: sticky;
  top: 0;
  z-index: var(--z-sidebar);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  padding: var(--s-3) var(--s-3) var(--s-3);
  padding-left: calc(var(--s-3) + var(--safe-left));
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  min-width: 0;
}
.sidebar__head {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  height: 40px;
  padding: 0 var(--s-1);
  margin-bottom: var(--s-1);
}
.brand {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  min-width: 0;
  flex: 1;
  border-radius: var(--r-sm);
}
.brand__mark {
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: 8px;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.brand__mark img { width: 100%; height: 100%; object-fit: contain; }
.brand__mark--default {
  background: var(--accent-gradient);
  color: #ffffff;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18), 0 4px 14px color-mix(in srgb, var(--accent) 35%, transparent);
}
.brand__mark--default .icon { width: 60%; height: 60%; stroke-width: 2.25; }
.brand__text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.brand__name { font-weight: var(--fw-semibold); font-size: var(--fs-md); letter-spacing: -0.01em; }
.brand__pack { font-size: var(--fs-xs); color: var(--text-3); }
.brand__name, .brand__pack { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sidebar__nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  margin: 0 calc(var(--s-1) * -1);
  padding: 0 var(--s-1) var(--s-2);
  overscroll-behavior: contain;
}
.sidebar__foot {
  display: flex;
  flex-direction: column;
  gap: var(--s-1);
  padding-top: var(--s-2);
  border-top: 1px solid var(--border);
}
.sidebar__tools { display: flex; align-items: center; gap: var(--s-1); }
.sidebar__tools .user-chip { flex: 1; min-width: 0; }
.sidebar__lang { padding: 0 8px; }
.sidebar__lang-code { font-size: var(--fs-xs); font-weight: var(--fw-semibold); letter-spacing: 0.04em; }

/* ---------- Navigation ---------- */
.nav-section + .nav-section { margin-top: var(--s-3); }
.nav-section__label {
  display: flex;
  align-items: center;
  height: 28px;
  padding: 0 var(--s-3);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--s-3);
  width: 100%;
  height: 34px;
  padding: 0 var(--s-3);
  border-radius: var(--r-sm);
  color: var(--text-2);
  font-size: var(--fs-base);
  font-weight: var(--fw-medium);
  text-align: left;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.nav-item .icon { color: var(--text-3); transition: color var(--dur-fast) var(--ease); }
.nav-item:hover { background: var(--surface-hover); color: var(--text); }
.nav-item:hover .icon { color: var(--text-2); }
.nav-item[aria-current="page"] { background: var(--accent-softer); color: var(--text); }
.nav-item[aria-current="page"] .icon { color: var(--accent-fg); }
.nav-item[aria-current="page"]::before {
  content: '';
  position: absolute;
  left: -4px;
  top: 8px;
  bottom: 8px;
  width: 2px;
  border-radius: 2px;
  background: var(--accent);
}
.nav-item__label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item__badge {
  flex: none;
  font-size: var(--fs-xs);
  font-variant-numeric: tabular-nums;
  color: var(--text-3);
}
.nav-item__live { width: 6px; height: 6px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 3px var(--success-soft); }

/* ---------- Connection switcher ---------- */
.conn-switch {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  width: 100%;
  min-height: 48px;
  padding: var(--s-2) var(--s-3);
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  text-align: left;
  transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.conn-switch:hover { border-color: var(--border-strong); background: var(--surface-2); }
.conn-switch__swatch {
  position: relative;
  width: 24px;
  height: 24px;
  flex: none;
  border-radius: 7px;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--conn-color, var(--accent)) 18%, transparent);
  color: var(--conn-color, var(--accent));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--conn-color, var(--accent)) 45%, transparent);
}
.conn-switch__swatch .icon { width: 14px; height: 14px; }
.conn-switch__text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.conn-switch__name { font-weight: var(--fw-semibold); font-size: var(--fs-sm); color: var(--text); }
.conn-switch__meta { font-family: var(--font-mono); font-size: 10.5px; color: var(--text-3); }
.conn-switch__name, .conn-switch__meta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn-switch__chev { color: var(--text-3); }
.conn-switch--empty .conn-switch__swatch { --conn-color: var(--muted); }

.status-dot {
  width: 8px;
  height: 8px;
  flex: none;
  border-radius: 50%;
  background: var(--muted);
}
.status-dot--ok { background: var(--success); box-shadow: 0 0 0 3px var(--success-soft); }
.status-dot--error { background: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }
.status-dot--checking { background: var(--warning); animation: dot-blink 1s ease-in-out infinite; }
.status-dot--live { background: var(--success); box-shadow: 0 0 0 0 var(--success-border); animation: dot-live 2.4s ease-out infinite; }
@keyframes dot-blink { 50% { opacity: 0.35; } }
@keyframes dot-live {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--success) 45%, transparent); }
  70%, 100% { box-shadow: 0 0 0 6px transparent; }
}

/* ---------- Search trigger ---------- */
.search-trigger {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  width: 100%;
  height: 34px;
  padding: 0 var(--s-2) 0 var(--s-3);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--bg);
  color: var(--text-3);
  font-size: var(--fs-sm);
  transition: border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.search-trigger:hover { border-color: var(--border-strong); color: var(--text-2); }
.search-trigger__label { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- User chip ---------- */
.user-chip {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  width: 100%;
  padding: var(--s-2);
  border-radius: var(--r-sm);
  text-align: left;
  transition: background var(--dur-fast) var(--ease);
}
.user-chip:hover { background: var(--surface-hover); }
.user-chip__text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.user-chip__name { font-size: var(--fs-sm); font-weight: var(--fw-semibold); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip__role { font-size: var(--fs-xs); color: var(--text-3); }
.avatar {
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: var(--fw-bold);
  letter-spacing: 0.02em;
  color: var(--accent-contrast);
  background: var(--accent-solid);
}
.avatar--sm { width: 24px; height: 24px; font-size: 10px; }
.avatar--lg { width: 40px; height: 40px; font-size: 14px; }
.avatar--neutral { background: var(--surface-3); color: var(--text-2); }

/* ---------- Icon rail: the sidebar is a size container, narrow (collapsed / tablet) → rail ---------- */
.sidebar { container: sidebar / inline-size; }
@container sidebar (max-width: 120px) {
  .sidebar__head { flex-direction: column; height: auto; gap: var(--s-2); padding: 0; }
  .brand { flex: none; }
  :is(.brand__text, .nav-item__label, .nav-item__badge, .conn-switch__text, .conn-switch__chev, .search-trigger__label, .search-trigger .kbd, .user-chip__text, .sidebar__lang-code) { display: none; }
  .nav-section__label { visibility: hidden; height: 12px; }
  .nav-item { justify-content: center; padding: 0; width: 40px; height: 40px; margin-inline: auto; }
  .nav-item__live { position: absolute; top: 8px; right: 8px; }
  .conn-switch { position: relative; width: 44px; min-height: 44px; padding: 0; justify-content: center; }
  .conn-switch .status-dot { position: absolute; top: 4px; right: 4px; }
  .search-trigger { width: 40px; height: 40px; padding: 0; justify-content: center; margin-inline: auto; }
  .sidebar__foot { align-items: center; }
  .sidebar__tools { flex-direction: column; }
  .sidebar__tools .user-chip { width: auto; flex: none; padding: var(--s-1); }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .app, .app[data-sidebar="collapsed"] { --sidebar-current: var(--sidebar-w-collapsed); }
  .sidebar .sidebar__collapse { display: none; }
}

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-topbar);
  display: flex;
  align-items: center;
  gap: var(--s-3);
  height: var(--topbar-h);
  padding: 0 var(--page-px);
  padding-right: calc(var(--page-px) + var(--safe-right));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: var(--backdrop-blur);
  -webkit-backdrop-filter: var(--backdrop-blur);
  border-bottom: 1px solid var(--border);
}
.topbar__mobile { display: none; }
.topbar__crumbs { flex: 1; min-width: 0; }
.topbar__right { display: flex; align-items: center; gap: var(--s-2); flex: none; }
.topbar .search-trigger--compact { width: 240px; height: var(--ctl-h-sm); background: var(--surface); }

.crumbs {
  display: flex;
  align-items: center;
  gap: var(--s-1);
  min-width: 0;
  font-size: var(--fs-sm);
  color: var(--text-3);
  white-space: nowrap;
}
.crumbs__item { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.crumbs__item:not(:last-child) { flex: 0 1 auto; }
.crumbs a { color: var(--text-3); border-radius: var(--r-xs); transition: color var(--dur-fast) var(--ease); }
.crumbs a:hover { color: var(--text); }
.crumbs__sep { flex: none; width: 14px; height: 14px; color: var(--text-3); opacity: 0.6; }
.crumbs__current { color: var(--text); font-weight: var(--fw-medium); }

.live-indicator {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  height: 28px;
  padding: 0 var(--s-3);
  border-radius: var(--r-full);
  border: 1px solid var(--border);
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  color: var(--text-2);
  white-space: nowrap;
}
.live-indicator.is-offline { color: var(--warning); border-color: var(--warning-border); background: var(--warning-soft); }

/* ---------- Content ---------- */
.content {
  flex: 1;
  min-width: 0;
  padding: var(--page-py) var(--page-px) var(--s-12);
  padding-right: calc(var(--page-px) + var(--safe-right));
}
.content:focus { outline: none; }
.route { min-width: 0; animation: route-in var(--dur) var(--ease); }
@keyframes route-in { from { opacity: 0; transform: translateY(4px); } }

/* ---------- Page ---------- */
.page { max-width: var(--content-max); margin: 0 auto; min-width: 0; }
.page__header {
  display: flex;
  align-items: flex-start;
  gap: var(--s-4);
  flex-wrap: wrap;
  margin-bottom: var(--s-6);
}
.page__heading { display: flex; align-items: flex-start; gap: var(--s-3); flex: 1 1 320px; min-width: 0; }
.page__icon {
  width: 40px;
  height: 40px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: var(--r);
  color: var(--tone-fg, var(--accent-fg));
  background: var(--tone-soft, var(--accent-soft));
  box-shadow: inset 0 0 0 1px var(--tone-border, var(--accent-border));
}
.page__icon .icon { width: 20px; height: 20px; }
.page__titles { display: flex; flex-direction: column; gap: var(--s-1); min-width: 0; }
.page__title {
  font-size: var(--fs-2xl);
  font-weight: var(--fw-semibold);
  letter-spacing: -0.02em;
  line-height: 1.2;
  overflow-wrap: anywhere;
}
.page__subtitle { color: var(--text-2); font-size: var(--fs-base); max-width: 72ch; }
.page__meta { display: flex; align-items: center; flex-wrap: wrap; gap: var(--s-2); margin-top: var(--s-2); }
.page__actions { display: flex; align-items: center; gap: var(--s-2); flex-wrap: wrap; }
.page__body { min-width: 0; display: flex; flex-direction: column; gap: var(--s-5); }
.page--narrow { max-width: 880px; }

/* ---------- Mobile tab bar ---------- */
.tabbar { display: none; }

/* ---------- Nav drawer (mobile/tablet) ---------- */
.nav-panel { display: flex; flex-direction: column; gap: var(--s-3); min-height: 100%; }
.nav-panel .nav-item { height: 44px; font-size: var(--fs-md); }
.nav-panel .nav-item[aria-current="page"]::before { left: 0; }
.nav-panel__foot { display: flex; flex-direction: column; gap: var(--s-2); padding-top: var(--s-3); border-top: 1px solid var(--border); margin-top: auto; }
.nav-panel__tools { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: var(--s-2); }

/* ---------- Phone ---------- */
@media (max-width: 767px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: none; }
  .topbar {
    height: calc(var(--mobile-bar-h) + var(--safe-top));
    padding: var(--safe-top) calc(var(--s-1) + var(--safe-right)) 0 calc(var(--s-1) + var(--safe-left));
    gap: var(--s-1);
  }
  .topbar__crumbs, .topbar__right { display: none; }
  .topbar__mobile { display: flex; align-items: center; gap: var(--s-1); flex: 1; min-width: 0; }
  .topbar__title {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: var(--s-2);
    font-size: var(--fs-md);
    font-weight: var(--fw-semibold);
  }
  .topbar__title-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .content {
    padding: var(--page-py) calc(var(--page-px) + var(--safe-right)) calc(var(--tabbar-h) + var(--safe-bottom) + var(--s-8)) calc(var(--page-px) + var(--safe-left));
  }
  .page__header { margin-bottom: var(--s-4); gap: var(--s-3); }
  .page__heading { flex-basis: 100%; }
  .page__title { font-size: var(--fs-xl); }
  .page__subtitle { font-size: var(--fs-sm); }
  .page__icon { width: 36px; height: 36px; }
  .page__actions { width: 100%; }

  .tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: var(--z-topbar);
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    height: calc(var(--tabbar-h) + var(--safe-bottom));
    padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
    background: color-mix(in srgb, var(--bg-elevated) 88%, transparent);
    backdrop-filter: var(--backdrop-blur);
    -webkit-backdrop-filter: var(--backdrop-blur);
    border-top: 1px solid var(--border);
  }
  .tabbar__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-width: 0;
    min-height: 44px;
    padding: 0 var(--s-1);
    color: var(--text-3);
    font-size: 10.5px;
    font-weight: var(--fw-medium);
    -webkit-tap-highlight-color: transparent;
    transition: color var(--dur-fast) var(--ease);
  }
  .tabbar__icon {
    display: grid;
    place-items: center;
    width: 48px;
    height: 28px;
    border-radius: var(--r-full);
    transition: background var(--dur-fast) var(--ease);
  }
  .tabbar__icon .icon { width: 20px; height: 20px; }
  .tabbar__label { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabbar__item[aria-current="page"] { color: var(--text); }
  .tabbar__item[aria-current="page"] .tabbar__icon { background: var(--accent-soft); color: var(--accent-fg); }
}
