@use '../../../shared/styles/settings-shared' as settings;

.main-container {
  @include settings.settings-page-container;
}

@include settings.settings-page-header;

.settings-title {
  @include settings.settings-page-title;
  margin-bottom: 0.75rem;
}

.settings-description {
  @include settings.settings-page-description;
}

.settings-card {
  @include settings.settings-card;
}

.section-header {
  @include settings.settings-section-header;
}

.section-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;

  @media (max-width: 767px) {
    flex-direction: column;
    align-items: stretch;

    p-button {
      align-self: flex-end;
    }
  }
}

.section-header-text {
  flex: 1;
}

.section-title {
  @include settings.settings-section-title;
}

.section-description {
  @include settings.settings-section-description;
}

.section-body {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

// Empty state
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem 2rem;
  text-align: center;
  color: var(--p-text-muted-color);
  background: color-mix(in srgb, var(--ground-background) 50%, var(--p-surface-800) 50%);
  border-radius: 6px;

  .pi {
    font-size: 2.5rem;
    margin-bottom: 1rem;
    opacity: 0.5;
  }

  .empty-title {
    font-size: 1rem;
    font-weight: 600;
    margin: 0 0 0.5rem 0;
    color: var(--p-text-color);
  }

  .empty-subtitle {
    font-size: settings.$settings-section-description-size;
    margin: 0;
  }
}

// Users list
.users-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.user-card {
  background: color-mix(in srgb, var(--ground-background) 50%, var(--p-surface-800) 50%);
  border: 1px solid var(--p-content-border-color);
  border-left: 3px solid var(--p-content-border-color);
  border-radius: 6px;
  transition: border-color 0.2s ease, background 0.2s ease;

  &:hover {
    background: color-mix(in srgb, var(--ground-background) 40%, var(--p-surface-800) 60%);
  }

  &.is-admin {
    border-left-color: #10b981;
  }

  &.is-expanded {
    border-left-color: var(--p-primary-color);
  }
}

.user-card-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;

  @media (max-width: 900px) {
    flex-wrap: wrap;
  }

  @media (max-width: 600px) {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }
}

.user-main-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
}

.expand-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--p-text-muted-color);
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s;
  flex-shrink: 0;

  &:hover {
    background: var(--p-surface-100);
    color: var(--p-primary-color);
  }

  .pi {
    font-size: 0.875rem;
  }
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--p-primary-color);
  color: var(--p-primary-contrast-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1rem;
  flex-shrink: 0;
}

.user-identity {
  flex: 1;
  min-width: 0;
}

.user-name-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.username {
  font-weight: 600;
  font-size: 1rem;
  color: var(--p-text-color);
}

.user-type-badge {
  display: inline-block;
  padding: 0.125rem 0.5rem;
  border: 1px solid;
  border-radius: 12px;
  font-size: 0.625rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.025em;

  &[data-type="LOCAL"] {
    color: #3b82f6;
    background: color-mix(in srgb, #3b82f6 15%, transparent);
    border-color: #3b82f6;
  }

  &[data-type="OIDC"] {
    color: #8b5cf6;
    background: color-mix(in srgb, #8b5cf6 15%, transparent);
    border-color: #8b5cf6;
  }
}

.admin-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.125rem 0.5rem;
  background: color-mix(in srgb, #10b981 15%, transparent);
  color: #10b981;
  border-radius: 12px;
  font-size: 0.625rem;
  font-weight: 700;
  text-transform: uppercase;

  .pi {
    font-size: 0.625rem;
  }
}

.user-meta {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  margin-top: 0.25rem;
  font-size: settings.$settings-description-size;
  color: var(--p-text-muted-color);
}

.meta-separator {
  opacity: 0.5;
}

.user-summary {
  flex-shrink: 0;

  @media (max-width: 600px) {
    width: 100%;
  }
}

.permission-badges {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;

  @media (max-width: 600px) {
    justify-content: flex-start;
  }
}

.permission-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.25rem 0.625rem;
  border-radius: 16px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--p-surface-100);
  border: 1px solid var(--p-content-border-color);
  transition: all 0.2s;

  .pi {
    font-size: 0.75rem;
  }

  &[data-level="none"] {
    background: color-mix(in srgb, #6b7280 15%, transparent);
    border-color: #6b7280;
    color: #6b7280;
  }

  &[data-level="low"] {
    background: color-mix(in srgb, #ef4444 15%, transparent);
    border-color: #ef4444;
    color: #ef4444;
  }

  &[data-level="medium"] {
    background: color-mix(in srgb, #f59e0b 15%, transparent);
    border-color: #f59e0b;
    color: #f59e0b;
  }

  &[data-level="high"] {
    background: color-mix(in srgb, #10b981 15%, transparent);
    border-color: #10b981;
    color: #10b981;
  }
}

.section-separator {
  width: 1px;
  height: 32px;
  background: var(--p-content-border-color);
  flex-shrink: 0;

  @media (max-width: 600px) {
    display: none;
  }
}

.user-actions {
  display: flex;
  gap: 0.375rem;
  flex-shrink: 0;

  @media (max-width: 600px) {
    justify-content: flex-end;
  }
}

// Expanded card body
.user-card-body {
  padding: 0 1rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  border-top: 1px dashed var(--p-content-border-color);
  margin-top: 0;
  padding-top: 1rem;
}

.user-details-section,
.permissions-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.content-restrictions-section {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 0.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--p-content-border-color);

  .section-description {
    font-size: settings.$settings-description-size;
    color: var(--p-text-muted-color);
    margin: 0 0 0.5rem 0;
    padding-left: 1.5rem;

    @media (max-width: 600px) {
      padding-left: 0;
    }
  }

  app-content-restrictions-editor {
    padding-left: 1.5rem;

    @media (max-width: 600px) {
      padding-left: 0;
    }
  }
}

.details-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: settings.$settings-label-size;
  font-weight: 600;
  color: var(--p-text-color);
  margin: 0;

  .pi {
    color: var(--p-primary-color);
    font-size: 1rem;
  }
}

.details-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  padding-left: 1.5rem;

  @media (max-width: 600px) {
    padding-left: 0;
  }
}

.detail-field {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;

  &.detail-field-wide {
    grid-column: span 2;

    @media (max-width: 600px) {
      grid-column: span 1;
    }
  }

  label {
    font-size: settings.$settings-description-size;
    font-weight: 500;
    color: var(--p-text-muted-color);
    text-transform: uppercase;
    letter-spacing: 0.025em;
  }

  span {
    font-size: settings.$settings-section-description-size;
    color: var(--p-text-color);
  }

  input {
    width: 100%;
  }
}

// Permissions grid
.permissions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1.25rem;
  padding-left: 1.5rem;

  @media (max-width: 600px) {
    padding-left: 0;
    grid-template-columns: 1fr;
  }
}

.permission-group {
  h5 {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: settings.$settings-description-size;
    font-weight: 600;
    color: var(--p-text-color);
    margin: 0 0 0.625rem 0;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--p-content-border-color);

    .pi {
      color: var(--p-primary-color);
      font-size: 0.875rem;
    }
  }
}

.permission-items {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.permission-item {
  display: flex;
  align-items: center;
  gap: 0.625rem;

  label {
    font-size: settings.$settings-description-size;
    color: var(--p-text-color);
    cursor: pointer;
    user-select: none;
  }

  p-checkbox {
    flex-shrink: 0;
  }
}

// Dialog styles
::ng-deep .user-dialog {
  .p-dialog {
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  }

  .p-dialog-header {
    position: relative;
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1.5rem;
    background: var(--p-content-background);
    border-bottom: 1px solid var(--p-content-border-color);
    border-radius: 10px 10px 0 0;

    .p-dialog-title {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      font-size: settings.$settings-section-title-size;
      font-weight: 600;
      color: var(--p-text-color);
      margin: 0;

      &::before {
        content: '\e923';
        font-family: 'primeicons';
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        background: var(--p-primary-color);
        color: var(--p-primary-contrast-color);
        border-radius: 8px;
        font-size: 1.25rem;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
      }
    }

    .p-dialog-header-icons {
      position: absolute;
      top: 1rem;
      right: 1rem;

      .p-dialog-header-close {
        width: 2rem;
        height: 2rem;
        border-radius: 50%;
        transition: all 0.2s;
        color: var(--p-text-muted-color);

        &:hover {
          background: var(--p-surface-100);
          color: var(--p-text-color);
        }
      }
    }
  }

  .p-dialog-content {
    padding: 1.5rem;
    background: var(--p-content-background);
  }

  .p-dialog-footer {
    padding: 1rem 1.5rem;
    background: var(--p-content-background);
    border-top: 1px solid var(--p-content-border-color);
    border-radius: 0 0 10px 10px;
  }
}

.dialog-form {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;

  label {
    font-size: settings.$settings-section-description-size;
    font-weight: 600;
    color: var(--p-text-color);
    margin-bottom: 0.25rem;
  }

  input,
  ::ng-deep .p-password {
    width: 100%;
  }

  ::ng-deep .p-inputtext,
  ::ng-deep .p-password-input {
    padding: 0.75rem;
    font-size: settings.$settings-label-size;
    border: 1px solid var(--p-content-border-color);
    border-radius: 6px;
    background: var(--p-content-background);
    color: var(--p-text-color);
    transition: all 0.2s;

    &:hover {
      border-color: var(--p-primary-color);
    }

    &:focus {
      outline: none;
      border-color: var(--p-primary-color);
      box-shadow: 0 0 0 3px color-mix(in srgb, var(--p-primary-color) 20%, transparent);
    }
  }
}

.error-message {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.875rem 1rem;
  background: color-mix(in srgb, #ef4444 12%, var(--p-content-background));
  border: 1px solid color-mix(in srgb, #ef4444 30%, transparent);
  border-left: 3px solid #ef4444;
  border-radius: 6px;
  color: #ef4444;
  font-size: settings.$settings-section-description-size;
  font-weight: 500;

  .pi {
    font-size: 1rem;
    flex-shrink: 0;
    margin-top: 0.125rem;
  }

  span {
    line-height: 1.5;
    flex: 1;
  }
}

.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.75rem;

  ::ng-deep .p-button {
    padding: 0.75rem 1.5rem;
    font-weight: 600;
    font-size: settings.$settings-label-size;
    border-radius: 6px;
    transition: all 0.2s;
    min-width: 120px;

    &.p-button-outlined {
      border-width: 1px;
      background: transparent;

      &:hover {
        background: var(--p-surface-100);
      }
    }

    &:not(.p-button-outlined) {
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

      &:hover {
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.18);
        transform: translateY(-1px);
      }

      &:active {
        transform: translateY(0);
      }
    }
  }
}

@media (max-width: 480px) {
  ::ng-deep .user-dialog {
    .p-dialog {
      width: 95vw !important;
      max-width: 95vw !important;
      margin: 0.5rem;
      border-radius: 8px;
    }

    .p-dialog-header {
      padding: 1rem;

      .p-dialog-title {
        font-size: 1rem;

        &::before {
          width: 36px;
          height: 36px;
          font-size: 1rem;
        }
      }
    }

    .p-dialog-content {
      padding: 1rem;
    }

    .p-dialog-footer {
      padding: 0.875rem 1rem;
    }
  }

  .dialog-actions {
    flex-direction: column-reverse;
    width: 100%;
    gap: 0.5rem;

    ::ng-deep .p-button {
      width: 100%;
      min-width: unset;
      justify-content: center;
    }
  }
}
