/**
 * Layout Components
 * @countermeasure/web-components
 * 
 * Shared layout styles for shell, sidebar, header, sheets, and user menu.
 */

/* ============================================================================
   PAGE SHELL
   ============================================================================ */
.cmm-shell {
  position: relative;
  display: flex;
  min-height: 100vh;
  background-color: hsl(var(--background));
  color: hsl(var(--foreground));
}

.cmm-shell__content {
  position: relative;
  display: flex;
  min-height: 100vh;
  width: 100%;
  flex-direction: column;
}

.cmm-app-shell {
  display: grid;
  min-height: 100vh;
  grid-template-columns: auto minmax(0, 1fr);
  background: var(--color-bg);
  color: var(--color-text);
}

.cmm-app-shell__sidebar {
  min-width: 0;
}

.cmm-app-shell__main {
  display: flex;
  min-width: 0;
  flex-direction: column;
}

.cmm-app-shell__content {
  width: 100%;
  min-width: 0;
  flex: 1;
  padding: var(--space-6);
}

.cmm-app-shell__metric-strip {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: var(--space-2);
}

@media (max-width: 1023px) {
  .cmm-app-shell {
    display: flex;
    flex-direction: column;
  }

  .cmm-app-shell__content {
    padding: var(--space-4);
  }

  .cmm-app-shell__metric-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.cmm-page-header {
  display: grid;
  gap: var(--space-3);
  max-width: 72rem;
  padding: var(--space-6) 0;
}

.cmm-page-header__top {
  display: flex;
  min-width: 0;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
}

.cmm-page-header__copy {
  display: grid;
  min-width: 0;
  gap: var(--space-2);
}

.cmm-page-header__eyebrow {
  font: var(--type-eyebrow);
  letter-spacing: var(--letter-eyebrow);
  text-transform: uppercase;
  color: var(--color-text-dim);
}

.cmm-page-header__title {
  margin: 0;
  max-width: 48rem;
  font: var(--type-h1);
  letter-spacing: var(--letter-tight);
  color: var(--color-text);
}

.cmm-page-header__title--tight {
  max-width: 40rem;
}

.cmm-page-header__lede {
  max-width: 56rem;
  margin: 0;
  font: var(--type-body);
  color: var(--color-text-muted);
}

.cmm-page-header__metadata {
  display: flex;
  min-width: 0;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  font: var(--type-caption);
  color: var(--color-text-dim);
}

.cmm-page-header__metadata-item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.cmm-page-header__metadata-item + .cmm-page-header__metadata-item::before {
  content: "/";
  color: var(--color-border);
}

.cmm-page-header__actions {
  display: inline-flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-2);
}

@media (max-width: 767px) {
  .cmm-page-header__top {
    flex-direction: column;
    align-items: stretch;
  }

  .cmm-page-header__actions {
    justify-content: flex-start;
  }
}

/* ============================================================================
   SIDEBAR
   ============================================================================ */
.cmm-sidebar {
  position: relative;
  z-index: var(--z-fixed);
  display: flex;
  height: 100vh;
  flex-direction: column;
  overflow-y: auto;
  backdrop-filter: blur(12px);
  transition: all var(--transition-slow) var(--ease-standard);
  width: var(--sidebar-width);
  scrollbar-width: thin;
  border-right: 1px solid hsl(var(--border));
  background-color: hsl(var(--card));
}

@media (min-width: 1024px) {
  .cmm-sidebar {
    position: sticky;
    top: 0;
    flex-shrink: 0;
  }
}

/* Scrollbar styling */
.dark .cmm-sidebar {
  scrollbar-color: hsl(var(--foreground) / 0.1) transparent;
}

:not(.dark) .cmm-sidebar {
  scrollbar-color: hsl(var(--foreground) / 0.1) transparent;
}

.cmm-sidebar::-webkit-scrollbar {
  width: 4px;
}

.cmm-sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.dark .cmm-sidebar::-webkit-scrollbar-thumb {
  background: hsl(var(--foreground) / 0.1);
  border-radius: 2px;
}

.dark .cmm-sidebar::-webkit-scrollbar-thumb:hover {
  background: hsl(var(--foreground) / 0.2);
}

:not(.dark) .cmm-sidebar::-webkit-scrollbar-thumb {
  background: hsl(var(--foreground) / 0.1);
  border-radius: 2px;
}

:not(.dark) .cmm-sidebar::-webkit-scrollbar-thumb:hover {
  background: hsl(var(--foreground) / 0.2);
}

/* Mobile sidebar */
@media (max-width: 1023px) {
  .cmm-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-xl);
  }

  .cmm-shell--sidebar-open .cmm-sidebar {
    transform: translateX(0);
  }
}

/* Collapsed sidebar */
.cmm-shell--sidebar-collapsed .cmm-sidebar {
  width: var(--sidebar-collapsed-width);
}

.cmm-shell--sidebar-collapsed .cmm-sidebar__label,
.cmm-shell--sidebar-collapsed .cmm-sidebar__heading,
.cmm-shell--sidebar-collapsed .cmm-sidebar__brand-text {
  display: none;
}

.cmm-sidebar__inner {
  display: flex;
  height: 100%;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem 0.75rem;
}

/* Sidebar Brand */
.cmm-sidebar__brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0 0.5rem;
}

.cmm-sidebar__brand-link {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
}

.cmm-sidebar__logo {
  display: inline-flex;
  height: 2.5rem;
  width: 2.5rem;
  align-items: center;
  justify-content: center;
  border-radius: 0.75rem;
  font-size: 0;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  background: transparent;
}

.cmm-sidebar__logo img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.cmm-sidebar__brand-text {
  line-height: 1.2;
}

.cmm-sidebar__title {
  font-weight: 700;
  letter-spacing: -0.025em;
  color: hsl(var(--foreground));
}

.cmm-sidebar__subtitle {
  font-size: 0.6875rem;
  color: hsl(var(--muted-foreground));
}

/* Sidebar Toggle */
.cmm-sidebar__toggle {
  display: flex;
  height: 2rem;
  width: 2rem;
  align-items: center;
  justify-content: center;
  border-radius: 0.5rem;
  transition: all var(--transition-normal);
  border: 1px solid hsl(var(--border));
  background-color: hsl(var(--secondary));
  color: hsl(var(--muted-foreground));
  cursor: pointer;
}

.cmm-sidebar__toggle:hover {
  background-color: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
}

.cmm-sidebar__toggle-icon {
  height: 1rem;
  width: 1rem;
  transition: transform var(--transition-slow);
}

.cmm-shell--sidebar-collapsed .cmm-sidebar__toggle-icon {
  transform: rotate(180deg);
}

/* Sidebar Navigation */
.cmm-sidebar__nav {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 0.125rem;
  overflow-y: auto;
  padding: 0.25rem 0;
}

.cmm-sidebar__group {
  padding: 0.75rem 0.75rem 0.25rem;
}

.cmm-sidebar__heading {
  font-size: 0.625rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: hsl(var(--muted-foreground));
}

.cmm-sidebar__divider {
  height: 1px;
  margin: 0.5rem 0;
  background: linear-gradient(to right, transparent, hsl(var(--border)), transparent);
}

/* Navigation Links */
.cmm-sidebar__link {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  border-radius: 0.75rem;
  padding: 0.625rem 0.75rem;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all var(--transition-normal);
  color: hsl(var(--muted-foreground));
  text-decoration: none;
}

.cmm-sidebar__link:hover {
  background-color: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
}

.cmm-sidebar__link:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px hsl(var(--ring) / 0.5);
}

/* Active link */
.cmm-sidebar__link--active {
  background: var(--color-primary-alpha);
  color: var(--color-text);
}

.cmm-sidebar__link--active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  height: 1.5rem;
  width: 3px;
  transform: translateY(-50%);
  border-radius: 0 9999px 9999px 0;
  background: var(--color-primary);
  box-shadow: 0 0 8px hsl(var(--primary) / 0.4);
}

.cmm-sidebar__link--active .cmm-sidebar__icon {
  background: var(--color-primary-alpha);
  color: var(--color-primary);
}

/* Navigation Icons */
.cmm-sidebar__icon {
  display: inline-flex;
  height: 2.25rem;
  width: 2.25rem;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  border-radius: 0.5rem;
  transition: all var(--transition-normal);
  background-color: hsl(var(--secondary));
  color: hsl(var(--muted-foreground));
}

.cmm-sidebar__icon svg {
  height: 1.125rem;
  width: 1.125rem;
}

.cmm-sidebar__link:hover .cmm-sidebar__icon {
  background-color: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
}

.cmm-sidebar__label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.875rem;
  font-weight: 500;
  line-height: 1.2;
}

/* Sidebar Footer */
.cmm-sidebar__footer {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid hsl(var(--border));
}

/* ============================================================================
   USER MENU
   ============================================================================ */
.cmm-user-menu {
  position: relative;
}

.cmm-user-menu__trigger {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.5rem;
  border-radius: 0.75rem;
  background: transparent;
  border: none;
  cursor: pointer;
  transition: background-color var(--transition-normal);
  text-align: left;
}

.cmm-user-menu__trigger:hover {
  background-color: hsl(var(--accent));
}

.cmm-user-menu__avatar {
  display: flex;
  height: 2.25rem;
  width: 2.25rem;
  align-items: center;
  justify-content: center;
  border-radius: 0.5rem;
  background: linear-gradient(to bottom right, hsl(var(--gradient-from)), hsl(var(--gradient-to)));
  color: white;
  font-weight: 600;
  font-size: 0.875rem;
}

.cmm-user-menu__info {
  flex: 1;
  overflow: hidden;
}

.cmm-user-menu__name {
  font-weight: 600;
  font-size: 0.875rem;
  color: hsl(var(--foreground));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cmm-user-menu__email {
  font-size: 0.75rem;
  color: hsl(var(--muted-foreground));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cmm-user-menu__chevron {
  height: 1rem;
  width: 1rem;
  color: hsl(var(--muted-foreground));
  transition: transform var(--transition-normal);
}

.cmm-user-menu[aria-expanded="true"] .cmm-user-menu__chevron {
  transform: rotate(180deg);
}

/* User Menu Dropdown */
.cmm-user-menu__dropdown {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  margin-bottom: 0.5rem;
  background-color: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: 0.75rem;
  box-shadow: var(--shadow-lg);
  z-index: var(--z-dropdown);
  opacity: 0;
  visibility: hidden;
  transform: translateY(0.5rem);
  transition: opacity var(--transition-normal), transform var(--transition-normal), visibility var(--transition-normal);
}

.cmm-user-menu[aria-expanded="true"] .cmm-user-menu__dropdown {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.cmm-user-menu__dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 0.875rem;
  color: hsl(var(--foreground));
  background: transparent;
  border: none;
  cursor: pointer;
  text-decoration: none;
  transition: background-color var(--transition-fast);
}

.cmm-user-menu__dropdown-item:hover {
  background-color: hsl(var(--accent));
}

.cmm-user-menu__dropdown-item:first-child {
  border-radius: 0.75rem 0.75rem 0 0;
}

.cmm-user-menu__dropdown-item:last-child {
  border-radius: 0 0 0.75rem 0.75rem;
}

.cmm-user-menu__dropdown-item--danger {
  color: hsl(var(--danger));
}

.cmm-user-menu__dropdown-item--danger:hover {
  background-color: hsl(var(--danger) / 0.1);
}

.cmm-user-menu__dropdown-divider {
  height: 1px;
  margin: 0.25rem 0;
  background-color: hsl(var(--border));
}

/* ============================================================================
   COLLAPSIBLES
   ============================================================================ */
.cmm-collapsible {
  overflow: hidden;
}

.cmm-collapsible--hidden {
  display: none;
}

.cmm-collapsible__trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.75rem 1rem;
  background: transparent;
  border: none;
  cursor: pointer;
  font-weight: 500;
  color: hsl(var(--foreground));
  transition: background-color var(--transition-fast);
}

.cmm-collapsible__trigger:hover {
  background-color: hsl(var(--accent));
}

.cmm-collapsible__icon {
  height: 1rem;
  width: 1rem;
  color: hsl(var(--muted-foreground));
  transition: transform var(--transition-normal);
}

.cmm-collapsible[data-open="true"] .cmm-collapsible__icon {
  transform: rotate(180deg);
}

.cmm-collapsible__content {
  padding: 0 1rem 1rem;
}

/* ============================================================================
   SHEETS / SIDE PANELS
   ============================================================================ */
.cmm-sheet {
  position: fixed;
  inset: 0 0 0 auto;
  z-index: var(--z-modal);
  width: var(--sheet-width);
  max-width: 100%;
  background-color: hsl(var(--card));
  border-left: 1px solid hsl(var(--border));
  box-shadow: var(--shadow-xl);
  transform: translateX(100%);
  transition: transform var(--transition-slow) var(--ease-standard);
  display: flex;
  flex-direction: column;
}

.cmm-sheet--open {
  transform: translateX(0);
}

.cmm-sheet--left {
  inset: 0 auto 0 0;
  border-left: none;
  border-right: 1px solid hsl(var(--border));
  transform: translateX(-100%);
}

.cmm-sheet--left.cmm-sheet--open {
  transform: translateX(0);
}

.cmm-sheet__backdrop {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-modal) - 1);
  background-color: var(--color-backdrop);
  backdrop-filter: blur(4px);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition-normal), visibility var(--transition-normal);
}

.cmm-sheet__backdrop--visible {
  opacity: 1;
  visibility: visible;
}

.cmm-sheet__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid hsl(var(--border));
}

.cmm-sheet__title {
  font-size: 1.125rem;
  font-weight: 600;
  color: hsl(var(--foreground));
}

.cmm-sheet__close {
  display: flex;
  height: 2rem;
  width: 2rem;
  align-items: center;
  justify-content: center;
  border-radius: 0.5rem;
  background: transparent;
  border: none;
  cursor: pointer;
  color: hsl(var(--muted-foreground));
  transition: background-color var(--transition-fast), color var(--transition-fast);
}

.cmm-sheet__close:hover {
  background-color: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
}

.cmm-sheet__body {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
}

.cmm-sheet__footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid hsl(var(--border));
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
}

/* Sheet loading state */
.cmm-sheet__loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem;
}

.cmm-sheet__error {
  text-align: center;
  padding: 2rem;
  color: hsl(var(--muted-foreground));
}

/* ============================================================================
   HEADER
   ============================================================================ */
.cmm-header {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  display: flex;
  height: var(--header-height);
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  border-bottom: 1px solid hsl(var(--border));
  background-color: hsl(var(--card) / 0.8);
  backdrop-filter: blur(12px);
}

.cmm-header__left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.cmm-header__right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.cmm-header__title {
  font-size: 1.125rem;
  font-weight: 600;
  color: hsl(var(--foreground));
}

/* Mobile menu toggle */
.cmm-header__menu-toggle {
  display: flex;
  height: 2.5rem;
  width: 2.5rem;
  align-items: center;
  justify-content: center;
  border-radius: 0.5rem;
  background: transparent;
  border: none;
  cursor: pointer;
  color: hsl(var(--foreground));
}

@media (min-width: 1024px) {
  .cmm-header__menu-toggle {
    display: none;
  }
}

.cmm-header__menu-toggle:hover {
  background-color: hsl(var(--accent));
}

/* ============================================================================
   ICON BUTTON
   ============================================================================ */
.cmm-icon-button {
  display: inline-flex;
  height: 2.25rem;
  width: 2.25rem;
  align-items: center;
  justify-content: center;
  border-radius: 0.5rem;
  transition: all var(--transition-normal);
  border: 1px solid hsl(var(--border));
  background-color: hsl(var(--secondary));
  color: hsl(var(--muted-foreground));
  cursor: pointer;
}

.cmm-icon-button:hover {
  background-color: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
}

.cmm-icon-button--ghost {
  border-color: transparent;
  background-color: transparent;
}

.cmm-icon-button--ghost:hover {
  background-color: hsl(var(--accent));
}

.cmm-icon-button svg {
  height: 1.25rem;
  width: 1.25rem;
}

/* ============================================================================
   THEME TOGGLE
   ============================================================================ */
.cmm-theme-toggle {
  position: relative;
  overflow: hidden;
}

.cmm-theme-toggle__icon {
  height: 1.25rem;
  width: 1.25rem;
  transition: all var(--transition-slow);
}

.cmm-theme-toggle__icon--dark {
  position: absolute;
  opacity: 0;
}

.dark .cmm-theme-toggle__icon--light,
[data-theme="dark"] .cmm-theme-toggle__icon--light {
  opacity: 0;
}

.dark .cmm-theme-toggle__icon--dark,
[data-theme="dark"] .cmm-theme-toggle__icon--dark {
  position: static;
  opacity: 1;
}

/* ============================================================================
   SIDEBAR BACKDROP (Mobile)
   ============================================================================ */
.cmm-sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-fixed) - 1);
  background-color: var(--color-backdrop);
  backdrop-filter: blur(4px);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition-normal), visibility var(--transition-normal);
}

@media (max-width: 1023px) {
  .cmm-shell--sidebar-open .cmm-sidebar-backdrop {
    opacity: 1;
    visibility: visible;
  }
}
