/* =========================================================================
   App Shell · Header · Sidebar · Content
   ========================================================================= */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  grid-template-rows: var(--header-height) 1fr;
  grid-template-areas:
    "sidebar header"
    "sidebar content";
  min-height: 100vh;
  background: var(--bg);
  transition: grid-template-columns var(--dur-normal) var(--ease-out);
}
.app-shell--collapsed { grid-template-columns: var(--sidebar-width-collapsed) minmax(0, 1fr); }

/* ---------- Header ---------- */
.app-shell__header {
  grid-area: header;
  position: sticky;
  top: 0;
  z-index: var(--z-header);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-6);
  background: var(--topbar-bg);
  color: var(--topbar-fg);
  border-block-end: 1px solid var(--topbar-border);
  height: var(--header-height);
}
.app-shell__header-left,
.app-shell__header-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.app-shell__header-left { flex: 0 0 auto; }
.app-shell__header-center { flex: 1 1 auto; min-width: 0; overflow: hidden; display: flex; align-items: center; gap: var(--space-3); }
.app-shell__header-right { flex: 0 0 auto; min-width: 0; flex-wrap: nowrap; }

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding-inline-end: var(--space-4);
}
.brand__mark {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent), var(--accent-tertiary));
  color: var(--text-on-accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: var(--fw-bold);
  letter-spacing: -0.02em;
}
.brand__name {
  font-size: var(--fs-body-lg);
  font-weight: var(--fw-semibold);
  letter-spacing: -0.01em;
  color: var(--text-primary);
}
.brand__tag {
  font-size: var(--fs-label-sm);
  color: var(--text-muted);
  font-weight: var(--fw-medium);
}

/* Header icon button */
.icon-btn {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.icon-btn:hover { background: var(--accent-soft); color: var(--text-primary); }
.icon-btn:active { transform: scale(0.96); }
.icon-btn__dot {
  position: absolute;
  top: 7px;
  inset-inline-end: 7px;
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--status-error);
  box-shadow: 0 0 0 2px var(--topbar-bg);
}

/* Global search */
.global-search {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 460px;
  overflow: hidden;
}
.health-icon { position: relative; }
.health-icon__dot {
  position: absolute;
  bottom: 7px;
  inset-inline-end: 7px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--status-success);
  border: 2px solid var(--topbar-bg);
}
.health-icon__dot::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  background: var(--status-success);
  opacity: 0.35;
  animation: ping 1.8s var(--ease-out) infinite;
}
.global-search__input {
  width: 100%;
  height: 38px;
  padding-inline: 40px var(--space-4);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  border: 1px solid transparent;
  color: var(--text-primary);
  transition: background var(--dur-fast), border-color var(--dur-fast), box-shadow var(--dur-fast);
}
.global-search__input::placeholder { color: var(--field-placeholder); }
.global-search__input:hover { background: var(--surface-3); }
.global-search__input:focus {
  background: var(--field-bg);
  border-color: var(--field-focus);
  box-shadow: var(--shadow-focus);
  outline: none;
}
.global-search__icon {
  position: absolute;
  inset-inline-start: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}
.global-search__kbd {
  position: absolute;
  inset-inline-end: 10px;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 2px 6px;
  border-radius: var(--radius-xs);
  background: var(--surface-3);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* Health pill */
.health-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 6px 12px;
  border-radius: var(--radius-full);
  background: var(--status-success-bg);
  color: var(--status-success-fg);
  font-size: var(--fs-label-md);
  font-weight: var(--fw-semibold);
}
.health-pill__dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--status-success);
  position: relative;
}
.health-pill__dot::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--status-success);
  animation: ping 1.6s var(--ease-out) infinite;
}
.health-pill--warning { background: var(--status-warning-bg); color: var(--status-warning-fg); }
.health-pill--warning .health-pill__dot { background: var(--status-warning); }
.health-pill--warning .health-pill__dot::after { background: var(--status-warning); }
.health-pill--error { background: var(--status-error-bg); color: var(--status-error-fg); }
.health-pill--error .health-pill__dot { background: var(--status-error); }
.health-pill--error .health-pill__dot::after { background: var(--status-error); }

/* Agent status menu */
.agent-status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 6px 10px 6px 8px;
  border-radius: var(--radius-full);
  background: var(--surface-2);
  color: var(--text-secondary);
  font-size: var(--fs-label-md);
  font-weight: var(--fw-medium);
  border: 1px solid var(--border);
  transition: background var(--dur-fast);
}
.agent-status:hover { background: var(--surface-3); }
.agent-status__dot {
  width: 10px;
  height: 10px;
  border-radius: var(--radius-full);
  background: var(--status-success);
}
.agent-status--busy .agent-status__dot { background: var(--status-warning); }
.agent-status--away .agent-status__dot { background: var(--status-neutral); }
.agent-status--dnd  .agent-status__dot { background: var(--status-error); }

/* User profile avatar in header */
.profile-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 4px 12px 4px 4px;
  border-radius: var(--radius-full);
  background: var(--surface-2);
  transition: background var(--dur-fast);
}
.profile-chip:hover { background: var(--surface-3); }
.profile-chip__avatar {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--accent-tertiary), var(--accent));
  color: var(--text-on-accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: var(--fw-semibold);
}
.profile-chip__name {
  font-size: var(--fs-label-md);
  font-weight: var(--fw-medium);
  color: var(--text-primary);
}

/* ---------- Sidebar ---------- */
.app-shell__sidebar {
  grid-area: sidebar;
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--sidebar-bg);
  color: var(--sidebar-fg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: var(--z-sticky);
}
.sidebar__brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--header-height);
  padding: 0 var(--space-5);
  border-block-end: 1px solid var(--sidebar-border);
  flex: 0 0 auto;
}
.sidebar__collapse-btn {
  margin-inline-start: auto;
  color: var(--text-muted);
}
.sidebar__nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4) var(--space-3);
}
.sidebar__group { margin-bottom: var(--space-5); }
.sidebar__group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: var(--fw-semibold);
  color: var(--sidebar-group-fg);
  cursor: pointer;
  border-radius: var(--radius-sm);
  user-select: none;
  transition: color var(--dur-fast);
}
.sidebar__group-header:hover { color: var(--sidebar-fg-strong); }
.sidebar__group-header .icon { transition: transform var(--dur-normal) var(--ease-out); }
.sidebar__group[data-collapsed="true"] .sidebar__group-header .icon { transform: rotate(-90deg); }
[dir="rtl"] .sidebar__group[data-collapsed="true"] .sidebar__group-header .icon { transform: rotate(90deg); }
.sidebar__group[data-collapsed="true"] .sidebar__items { display: none; }

.sidebar__items {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: var(--space-1);
}
.sidebar__item {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 8px var(--space-3);
  border-radius: var(--radius-sm);
  font-size: var(--fs-label-md);
  font-weight: var(--fw-medium);
  color: var(--sidebar-fg);
  transition: background var(--dur-fast), color var(--dur-fast);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar__item:hover {
  background: var(--sidebar-item-hover);
  color: var(--sidebar-fg-strong);
  text-decoration: none;
}
.sidebar__item.is-active {
  background: var(--sidebar-item-active-bg);
  color: var(--sidebar-item-active-fg);
  font-weight: var(--fw-semibold);
}
.sidebar__item.is-active .sidebar__indicator { opacity: 1; }
.sidebar__indicator {
  position: absolute;
  top: 6px;
  bottom: 6px;
  inset-inline-start: 0;
  width: 3px;
  border-radius: 0 var(--radius-xs) var(--radius-xs) 0;
  background: var(--sidebar-indicator);
  opacity: 0;
  transition: opacity var(--dur-fast);
}
[dir="rtl"] .sidebar__indicator { border-radius: var(--radius-xs) 0 0 var(--radius-xs); }
.sidebar__item .icon {
  flex: 0 0 auto;
  color: inherit;
  opacity: 0.85;
}
.sidebar__item-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar__item-badge {
  flex: 0 0 auto;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: var(--radius-full);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: var(--fw-semibold);
}

/* Sidebar footer */
.sidebar__footer {
  flex: 0 0 auto;
  padding: var(--space-3);
  border-block-start: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.sidebar__footer .sidebar__item {
  cursor: pointer;
  background: transparent;
  border: 0;
  text-align: start;
  width: 100%;
}

/* Workspace separator */
.sidebar__workspace {
  padding: var(--space-3) var(--space-4) var(--space-1) !important;
  margin: var(--space-3) 0 0 !important;
  font-size: 10px !important;
  font-weight: 700 !important;
  letter-spacing: 0.14em !important;
  text-transform: uppercase;
  color: var(--text-muted) !important;
  border: 0 !important;
  opacity: 0.65;
}
.sidebar__workspace:first-child { margin-top: 0 !important; }

/* Active group header */
.sidebar__group-header.is-active {
  color: var(--sidebar-fg-strong);
}
.sidebar__group-header.is-active > span:first-child > span:last-child {
  font-weight: var(--fw-semibold);
}

/* Collapsed sidebar */
.app-shell--collapsed .sidebar__group-header,
.app-shell--collapsed .sidebar__item-label,
.app-shell--collapsed .sidebar__item-badge,
.app-shell--collapsed .brand__name,
.app-shell--collapsed .brand__tag {
  display: none;
}
.app-shell--collapsed .sidebar__item { justify-content: center; padding-inline: 0; }
.app-shell--collapsed .sidebar__brand { padding-inline: var(--space-3); justify-content: center; }

/* ---------- Content ---------- */
.app-shell__content {
  grid-area: content;
  min-width: 0;
  padding: var(--gutter);
  overflow-x: hidden;
}
.page {
  max-width: var(--content-max);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--gutter);
  animation: fade-in var(--dur-normal) var(--ease-out) both;
}

/* Page header */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-5);
  flex-wrap: wrap;
}
.page-header__titles { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.page-header__title {
  font-size: var(--fs-headline-md);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  letter-spacing: -0.01em;
}
.page-header__subtitle {
  font-size: var(--fs-body-md);
  color: var(--text-secondary);
}
.page-header__actions { display: flex; gap: var(--space-2); align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.page-header__title { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; }

/* Breadcrumb */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--fs-label-md);
  color: var(--text-muted);
}
.breadcrumb__item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}
.breadcrumb__item a { color: var(--text-secondary); }
.breadcrumb__item:last-child { color: var(--text-primary); font-weight: var(--fw-medium); }
.breadcrumb__separator {
  color: var(--text-muted);
  font-size: 14px;
  opacity: 0.7;
}

/* Quick actions popover anchor */
.quick-actions-menu {
  position: absolute;
  inset-inline-end: var(--space-4);
  top: calc(var(--header-height) + var(--space-2));
  min-width: 280px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: var(--space-2);
  z-index: var(--z-drawer);
  animation: slide-up var(--dur-fast) var(--ease-out);
}
.quick-actions-menu[hidden] { display: none; }

/* Responsive */
@media (max-width: 1024px) {
  .app-shell { grid-template-columns: var(--sidebar-width-collapsed) minmax(0, 1fr); }
  .global-search { max-width: 320px; }
}
@media (max-width: 768px) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "header"
      "content";
    min-width: 0;
  }
  .app-shell__sidebar {
    position: fixed;
    inset-inline-start: 0;
    top: 0;
    width: var(--sidebar-width);
    z-index: var(--z-drawer);
    transform: translateX(-100%);
    transition: transform var(--dur-normal) var(--ease-out);
  }
  [dir="rtl"] .app-shell__sidebar { transform: translateX(100%); }
  .app-shell.is-mobile-open .app-shell__sidebar { transform: translateX(0); }

  .app-shell__header {
    padding: 0 var(--space-3);
    gap: var(--space-2);
    min-width: 0;
  }
  .app-shell__header-left, .app-shell__header-right { flex: 0 0 auto; gap: 4px; }
  .app-shell__header-center { min-width: 0; }

  /* Hide secondary header controls on mobile */
  .app-shell__header-right .icon-btn[title*="health" i],
  .app-shell__header-right .icon-btn[title*="systemHealth" i],
  .app-shell__header-right .icon-btn[aria-label*="health" i],
  .app-shell__header-right .icon-btn[title*="quickActions" i],
  .app-shell__header-right .icon-btn[aria-label*="quickActions" i],
  .app-shell__header-right .icon-btn[title*="lang" i],
  .app-shell__header-right .icon-btn[title*="theme" i],
  .app-shell__header-right .icon-btn[title*="help" i],
  .app-shell__header-right .agent-status { display: none; }

  /* Collapse search to icon-only */
  .global-search {
    min-width: 40px;
    max-width: 40px;
    flex: 0 0 40px;
  }
  .global-search__input,
  .global-search__kbd { display: none; }
  .global-search::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: var(--radius-md);
    background: var(--surface-2);
    cursor: pointer;
  }

  .health-pill__label,
  .agent-status span:not(.agent-status__dot),
  .profile-chip__name { display: none; }

  .app-shell__content { padding: var(--space-3); }
  .page { gap: var(--space-3); }
  .page-header__title { font-size: var(--fs-headline-sm); }
}
