// ============================================================================
// Agent Session Drawer — CUB-26
// Desktop: 480px side drawer slides from right with modal overlay.
// Mobile: Bottom sheet slides up from bottom.
// Uses Control Center design tokens from CUB-21.
// ============================================================================

// ---------------------------------------------------------------------------
// Backdrop
// ---------------------------------------------------------------------------
.session-drawer-backdrop {
  position: fixed;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 998;
  opacity: 0;
  transition: opacity 200ms ease-out;

  &--visible {
    opacity: 1;
  }
}

// ---------------------------------------------------------------------------
// Drawer Panel — Desktop: Side drawer from right
// ---------------------------------------------------------------------------
.session-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 480px;
  max-width: 100vw;
  background-color: var(--cc-surface-container);
  border-left: 1px solid var(--cc-outline);
  z-index: 999;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 280ms cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: -4px 0 32px rgba(0, 0, 0, 0.4);
  overflow: hidden;

  &--open {
    transform: translateX(0);
  }

  // ---------------------------------------------------------------------------
  // Mobile: Bottom Sheet — slides up from bottom
  // ---------------------------------------------------------------------------
  &--mobile {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    max-width: 100vw;
    max-height: 85vh;
    border-left: none;
    border-top: 1px solid var(--cc-outline);
    border-radius: 20px 20px 0 0;
    transform: translateY(100%);
    box-shadow: 0 -4px 32px rgba(0, 0, 0, 0.4);

    &.session-drawer--open {
      transform: translateY(0);
    }

    // Drag handle for mobile bottom sheet
    &::before {
      content: '';
      display: block;
      width: 32px;
      height: 4px;
      border-radius: 2px;
      background: var(--cc-on-surface-variant);
      opacity: 0.4;
      margin: 8px auto 4px;
    }
  }
}

// ---------------------------------------------------------------------------
// Header
// ---------------------------------------------------------------------------
.session-drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--cc-outline);
  flex-shrink: 0;
}

.session-drawer__header-identity {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1;
}

.session-drawer__header-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.session-drawer__title {
  font-size: 20px;
  font-weight: 600;
  color: var(--cc-on-surface);
  margin: 0;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-drawer__role {
  font-size: 13px;
  color: var(--cc-on-surface-variant);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-drawer__close-btn {
  --mat-icon-button-state-layer-color: transparent;
  color: var(--cc-on-surface-variant);
  flex-shrink: 0;

  &:hover {
    color: var(--cc-on-surface);
  }

  &:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
  }
}

// ---------------------------------------------------------------------------
// Content — scrollable area
// ---------------------------------------------------------------------------
.session-drawer__content {
  flex: 1;
  overflow-y: auto;
  padding: 0 24px 16px;
}

// ---------------------------------------------------------------------------
// Sections
// ---------------------------------------------------------------------------
.session-drawer__section {
  padding: 16px 0;
  border-bottom: 1px solid var(--cc-outline);

  &:last-child {
    border-bottom: none;
  }
}

.session-drawer__section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--cc-on-surface-variant);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0 0 12px;
}

// ---------------------------------------------------------------------------
// Meta Row — Status + Channel
// ---------------------------------------------------------------------------
.session-drawer__meta-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.session-drawer__status-chip {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;

  &.status-chip--active {
    background-color: var(--status-active-bg);
    color: var(--status-active);
  }

  &.status-chip--idle {
    background-color: var(--status-idle-bg);
    color: var(--status-idle);
  }

  &.status-chip--thinking {
    background-color: var(--status-thinking-bg);
    color: var(--status-thinking);
  }

  &.status-chip--error {
    background-color: var(--status-error-bg);
    color: var(--status-error);
  }

  &.status-chip--offline {
    background-color: rgba(100, 116, 139, 0.12);
    color: var(--status-offline);
  }
}

.session-drawer__channel-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 8px;
  background: var(--cc-surface-container-high);
  font-size: 12px;
  color: var(--cc-on-surface-variant);
}

.session-drawer__channel-icon {
  font-size: 14px;
  width: 14px;
  height: 14px;
}

// ---------------------------------------------------------------------------
// Session Key
// ---------------------------------------------------------------------------
.session-drawer__session-key {
  margin-bottom: 12px;
}

.session-drawer__label {
  display: block;
  font-size: 11px;
  font-weight: 500;
  color: var(--cc-on-surface-variant);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}

.session-drawer__key-value {
  display: block;
  font-family: var(--cc-font-mono);
  font-size: 12px;
  color: var(--color-secondary);
  background: var(--cc-surface);
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--cc-outline);
  word-break: break-all;
  line-height: 1.5;
  user-select: all;
}

// ---------------------------------------------------------------------------
// Task Info
// ---------------------------------------------------------------------------
.session-drawer__task-info {
  margin-bottom: 12px;
}

.session-drawer__task-text {
  font-size: 14px;
  color: var(--cc-on-surface);
  line-height: 1.4;
}

// ---------------------------------------------------------------------------
// Last Activity
// ---------------------------------------------------------------------------
.session-drawer__last-activity {
  margin-bottom: 0;
}

.session-drawer__activity-time {
  font-size: 14px;
  color: var(--cc-on-surface);
}

// ---------------------------------------------------------------------------
// Recent Messages
// ---------------------------------------------------------------------------
.session-drawer__messages {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.session-drawer__message {
  padding: 10px 14px;
  border-radius: 12px;
  max-width: 100%;

  &--agent {
    background: var(--cc-surface-container-high);
    border: 1px solid var(--cc-outline);
  }

  &--user {
    background: rgba(56, 189, 248, 0.08);
    border: 1px solid rgba(56, 189, 248, 0.15);
  }
}

.session-drawer__message-sender {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--cc-on-surface-variant);
  margin-bottom: 4px;
}

.session-drawer__message-text {
  font-size: 14px;
  color: var(--cc-on-surface);
  margin: 0;
  line-height: 1.5;
  word-wrap: break-word;
}

.session-drawer__message-time {
  display: block;
  font-size: 11px;
  color: var(--cc-on-surface-variant);
  margin-top: 4px;
  font-family: var(--cc-font-mono);
}

// ---------------------------------------------------------------------------
// Live Log Container
// ---------------------------------------------------------------------------
.session-drawer__log-container {
  background: var(--cc-surface);
  border: 1px solid var(--cc-outline);
  border-radius: 10px;
  padding: 10px 12px;
  max-height: 260px;
  overflow-y: auto;
  font-family: var(--cc-font-mono);
  font-size: 12px;
  line-height: 1.7;
}

.session-drawer__log-line {
  display: flex;
  gap: 8px;
  padding: 1px 0;
  white-space: nowrap;

  &--info {
    color: var(--cc-on-surface);
  }

  &--warn {
    color: #FBBF24;
  }

  &--error {
    color: var(--status-error);
  }

  &--debug {
    color: var(--cc-on-surface-variant);
  }
}

.session-drawer__log-time {
  color: var(--cc-on-surface-variant);
  flex-shrink: 0;
  opacity: 0.7;
}

.session-drawer__log-level {
  width: 48px;
  flex-shrink: 0;
  font-weight: 600;
  font-size: 11px;
  letter-spacing: 0.02em;
}

.session-drawer__log-message {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: inherit;
}

// ---------------------------------------------------------------------------
// Empty State
// ---------------------------------------------------------------------------
.session-drawer__empty-state {
  font-size: 13px;
  color: var(--cc-on-surface-variant);
  text-align: center;
  padding: 16px 0;
  margin: 0;
}

// ---------------------------------------------------------------------------
// Action Buttons — Sticky Footer
// ---------------------------------------------------------------------------
.session-drawer__actions {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 24px 20px;
  border-top: 1px solid var(--cc-outline);
  background-color: var(--cc-surface-container);
  flex-shrink: 0;
}

.session-drawer__action-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;

  .mat-icon {
    width: 18px;
    height: 18px;
    font-size: 18px;
  }

  &--primary {
    flex: 1;
  }

  &--secondary {
    flex-shrink: 0;
  }

  &:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
  }
}

// ---------------------------------------------------------------------------
// Mobile Adjustments
// ---------------------------------------------------------------------------
.session-drawer--mobile {
  .session-drawer__header {
    padding: 12px 20px 12px;
  }

  .session-drawer__content {
    padding: 0 20px 12px;
  }

  .session-drawer__actions {
    padding: 12px 20px 16px;
    flex-direction: column;

    .session-drawer__action-btn {
      width: 100%;
      justify-content: center;
    }
  }

  .session-drawer__log-container {
    max-height: 200px;
  }
}

// ---------------------------------------------------------------------------
// Responsive — wider viewports keep 480px, narrow go full-width
// ---------------------------------------------------------------------------
@media (max-width: 599px) {
  .session-drawer:not(.session-drawer--mobile) {
    width: 100%;
    max-width: 100vw;
  }
}

// ---------------------------------------------------------------------------
// Accessibility: Reduced Motion
// ---------------------------------------------------------------------------
@media (prefers-reduced-motion: reduce) {
  .session-drawer {
    transition: none;
  }

  .session-drawer-backdrop {
    transition: none;
  }
}